drager / faker

A library for Dart that generates fake data.
MIT License
231 stars 54 forks source link

Multiple fakers in a list #68

Closed RicardoMudinyane closed 2 months ago

RicardoMudinyane commented 1 year ago

I am trying to create multiple users with lastName, firstName, email and so on... I have a for loop and call faker each time, but it's creating the same user multiple times. I expected it to create multiple users with different names and so on...

Code snippet

for (int i = 0; i < 10; i++){
   var faker = Fake();

  users.add(
    Users(
      userid: faker.person.random.string(10),
      name: faker.person.firstName(),
      surname: faker.person.lastName(),
      phone: faker.phoneNumber.us(),
      email: faker.internet.email(),
      username: faker.internet.userName(),
    )
  );
}
jmartins-sh commented 1 year ago

Hi @RicardoMudinyane, how are you doing?

I've run this code:

import 'package:faker/faker.dart';

void main() {
  final List<User> users = [];

  for (int i = 0; i < 10; i++) {
    var faker = Faker();

    users.add(User(
      name: faker.person.firstName(),
      lastName: faker.person.lastName(),
    ));
  }

  for (var user in users) {
    print('${user.name} ${user.lastName}');
  }
}

class User {
  String name;
  String lastName;

  User({required this.name, required this.lastName});
}

And got this output:

image

Are you still facing the error?