As a Flutter developer, I've explored various tools to optimize my workflow. One standout tool has been ChatGPT. While the obvious use is code generation, I've found a unique application: generating mock data for real-world scenarios.

Here’s how ChatGPT helped me generate mock data for my Flash Template, especially during prototyping.

The Prompt

“Please generate mock data for a restaurant app, including name, description, address, zip code, city, image URL, and opening hours. Create the Dart code with a model class and a listview to display the restaurant list.”

The Output

The response from ChatGPT was incredible! It provided a set of fictional restaurants, each with unique names, descriptions, and varied opening hours. The data served as an excellent placeholder for backend development while also testing the UI design and responsiveness.

Here’s how I integrated the code into the template:

The Code

1. Model Definition

Create a restaurant.dart file with the following Restaurant model:

class Restaurant {
  final String name;
  final String description;
  final String address;
  final String zipCode;
  final String city;
  final String imageUrl;
  final String openingHours;

  Restaurant({
    required this.name,
    required this.description,
    required this.address,
    required this.zipCode,
    required this.city,
    required this.imageUrl,
    required this.openingHours,
  });
}

2. Generating Mock Data

In a separate file like mock_restaurant_data.dart, generate your mock data like this:

List<Restaurant> mockRestaurants = [
  Restaurant(
    name: "Cafe Del Mar",
    description: "A cozy place by the river with live music.",
    address: "Riverside Street 42",
    zipCode: "1010",
    city: "Vienna",
    imageUrl: "https://example.com/cafe_del_mar.jpg",
    openingHours: "Mon-Sun: 8AM - 11PM",
  ),
  // Add more restaurant data
];

3. Displaying the Data

Create a restaurant_list.widget.dart file to display this data in a list view:

class RestaurantList extends StatelessWidget {
  final List<Restaurant> restaurants;

  RestaurantList({required this.restaurants});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: restaurants.length,
      itemBuilder: (context, index) {
        final restaurant = restaurants[index];
        return ListTile(
          leading: Image.network(restaurant.imageUrl),
          title: Text(restaurant.name),
          subtitle: Text(restaurant.description),
        );
      },
    );
  }
}

Result

Here's how it looks in action:

Screenshots of the app displaying the restaurant list would go here.

I used this Flutter Flash Template to set up the UI quickly, and in just one afternoon, I had a working prototype. The mock data helped me visualize the structure, and ChatGPT saved hours by providing realistic inputs instantly.

Summary

Using ChatGPT for generating mock data enriched my development process, allowing me to focus on fine-tuning the app’s design. It sped up testing by providing realistic data scenarios, and in case I wasn’t happy with the result, I could always regenerate the data.

Time saved: Hours
Effort: Minimal

If you’re working on Flutter projects and need a fast, efficient way to launch, check out my Flutter Flash Template. You can release your app in days instead of weeks!

You can get the template here: Flutter Flash Template 🚀

Thanks for reading! Don’t forget to follow me on LinkedIn or Twitter for more tips on Flutter development.

Keep reading