Then, create a service that returns a new user. Here, I'll use abstraction to obey Open/Close Principle.
abstractclassIUserService{UserModelgetNewUser();}classUserServiceImplextendsIUserService {final faker =Faker();@overrideUserModelgetNewUser() {String city = faker.address.city();String name = faker.person.name();DateTime birthDate = faker.date.dateTimeBetween(DateTime(1940), DateTime.now());int age =DateTime.now().year - birthDate.year;String job = faker.job.title();returnUserModel( name: name, age: age.toString(), job: job, city: city, ); }}classUserServiceImplextendsIUserService {final faker =Faker();@overrideUserModelgetNewUser() {String city = faker.address.city();String name = faker.person.name();DateTime birthDate = faker.date.dateTimeBetween(DateTime(1940), DateTime.now());int age =DateTime.now().year - birthDate.year;String job = faker.job.title();returnUserModel( name: name, age: age.toString(), job: job, city: city, ); }}
After that, you are ready to create a ViewModel which extends Pulse's PulseListViewModel. We'll pass IUserService in constructor to do dependency inversion.
class UsersViewModel extends PulseListViewModel<UserModel> { // specify your data type so that you won't get confused with type conversion
finalIUserService service;UsersViewModel({required this.service, });voidaddUser() {UserModel user = service.getNewUser();addValue(user); // when you want to add something, use `addValue(T)` method from Pulse// it'll automatically add your data to list }}
Lastly, we'll have to create a view that shows a list of users.
classUsersViewextendsStatelessWidget {UsersView({Key? key}) : super(key: key); final viewModel = UsersViewModel(service: UserServiceImpl()); // initialize your view model, later we'll use Pulse's service locator
@overrideWidgetbuild(BuildContext context) {returnScaffold( appBar:AppBar( title:constText('Users'), ), body:PulseBuilder( // reactive when added a new user viewModel: viewModel, builder: (context, model, _) {return model.value.isEmpty?constCenter( child:Text('No users created!'), ):GridView.builder( padding:constEdgeInsets.only(bottom:80), addAutomaticKeepAlives:true, addRepaintBoundaries:true, gridDelegate:constSliverGridDelegateWithFixedCrossAxisCount( crossAxisCount:2, ), itemCount: model.value.length, itemBuilder: (context, index) {UserModel user = model.value[index];returnContainer( decoration:BoxDecoration( borderRadius:BorderRadius.circular(16.0), color:Colors.amber.shade200, ), margin:constEdgeInsets.all(8.0), padding:constEdgeInsets.all(8.0), child:Column( mainAxisAlignment:MainAxisAlignment.spaceEvenly, children: [Text( user.name, maxLines:8, style:Theme.of(context).textTheme.titleMedium, ),Text('${user.age} years', ),Text( user.job, textAlign:TextAlign.center, ),Text( user.city, ), ], ), ); }, ); }, ), floatingActionButton:FloatingActionButton( onPressed: viewModel.addUser, // add a new user child:constIcon(Icons.person_add_alt_rounded), ), ); }}
🍾Congratulations!! You've completed list data management with Pulse.