A month of Flutter: real faker data

Yesterday I left the code base with failing tests because the fixture data stored in a JSON file can't be read in Flutter tests. Today I'm going to complete the move to streamed mock data by switching from static fixtures to dynamic factories in a Dart file. The Unsplash image IDs will still be hard coded but the rest of the values will be dynamically generated.

The core of the work will be performed in mockPostData. This will create a Map with a random UUID from the uuid package, a random image ID from the pre-selected list, a DateTime, and random text and username with the faker package.

I have added a day between each DateTime value, so if I generate three posts, they will be from today, yesterday, and two days ago, respectively.

Map<String, dynamic> mockPostData({int index = 0}) {
  const Faker faker = Faker();
  final String imageId = faker.randomGenerator.element(imageIds);
  final String createdAt =
      DateTime.now().subtract(Duration(days: index)).toIso8601String();

   return <String, dynamic>{
    'id': Uuid().v4(),
    'imageUrl': 'https://source.unsplash.com/$imageId',
    'createdAt': createdAt,
    'text': faker.conference.name(),
    'username': faker.person.name()
  };
}

There are also two helper methods, mockPost and mockPosts, to wrap the raw data in Post instances. Now it's just a matter of updating the tests and the _loadPosts function to use the new mock methods.

Using faker to generate mock data results in different names showing up.

Screenshot of random mock usernames

This is the base mock data rendering. Tomorrow starts the fun work of rendering the actual images.

Code changes

Posts in this series

  • A month of Flutter
  • A month of Flutter: create the app
  • A month of Flutter: configuring continuous integration
  • A month of Flutter: continuous linting
  • A month of Flutter: upgrading to 1.0
  • A month of Flutter: initial theme
  • A month of Flutter: no content widget
  • A month of Flutter: a list of posts
  • A month of Flutter: extract post item widget
  • A month of Flutter: post model and mock data
  • A month of Flutter: rendering a ListView with StreamBuilder
  • A month of Flutter: Stream transforms and failing tests
  • A month of Flutter: real faker data
  • A month of Flutter: rendering network images
  • A month of Flutter: FABulous authentication
  • A month of Flutter: configure Firebase Auth for Sign in with Google on Android
  • A month of Flutter: configure Firebase Auth for Sign in with Google on iOS
  • A month of Flutter: Sign in with Google
  • A month of Flutter: mocking Firebase Auth in tests
  • A month of Flutter: delicious welcome snackbar
  • A month of Flutter: navigate to user registration
  • A month of Flutter: user registration form
  • A month of Flutter: testing forms
  • A month of Flutter: setting up Firebase Firestore
  • A month of Flutter: awesome adaptive icons
  • A month of Flutter: set up Firestore rules tests
  • A month of Flutter: Firestore create user rules and tests
  • A month of Flutter: WIP save users to Firestore
  • A month of Flutter: user registration refactor with reactive scoped model
  • A month of Flutter: the real hero animation
  • A month of Flutter: a look back

  • Category: Development