What is copyWith and how can I use it in Flutter and what are it's use cases in real world?

14,228

Solution 1

Let's say you have an object in which you want to change some properties. One way to do is by changing each property at a time like object.prop1 = x object.prop2 = y and so on. This will go cumbersome if you have more than few properties to change. Then copyWith method comes handy. This method takes all the properties(which need to change) and their corresponding values and returns new object with your desired properties.

updateWith method is doing same thing by again calling copyWith method and at the end it is pushing returned object to stream.

Solution 2

Lets say you have a class like it:

class PostSuccess {
  final List<Post> posts;
  final bool hasReachedMax;

  const PostSuccess({this.posts, this.hasReachedMax});

functionOne(){
///Body here

} 
}

Lets say you want to change some properties of the class on the go, What you can do you can declare the copyWith method like that:

PostSuccess copyWith({
    List<Post>? posts,
    bool? hasReachedMax,
  }) {
    return PostSuccess(
      posts: posts ?? this.posts,
      hasReachedMax: hasReachedMax ?? this.hasReachedMax,
    );
  }
  

As you see, in the return section, you can change the value of the properties as required by your situation and return the object.

Share:
14,228
Milan Poudel
Author by

Milan Poudel

Updated on June 14, 2022

Comments

  • Milan Poudel
    Milan Poudel almost 2 years
    //File: email_sign_in_model.dart
    
    class EmailSignInModel {
      EmailSignInModel({
        this.email='',
        this.formType=EmailSignInFormType.signIn,
        this.isLoading=false,
        this.password='',
        this.submitted=false,
      });
    
      final String email;
      final String password;
      final EmailSignInFormType formType;
      final bool isLoading;
      final bool submitted;
    
      EmailSignInModel copyWith({
        String email,
        String password,
        EmailSignInFormType formType,
        bool isLoading,
        bool submitted,
    
      }) {
        return EmailSignInModel(
        email: email ?? this.email,
        password: password?? this.password,
        formType: formType?? this.formType,
        isLoading: isLoading?? this.isLoading,
        submitted: submitted?? this.submitted
    
        );
      }
    }
    
    
    
    //File: email_sign_in_bloc.dart
    
    import 'dart:async';
    import 'package:timetrackerapp/app/sign_in/email_sign_in_model.dart';
    
    class EmailSignInBloc {
     final StreamController<EmailSignInModel> _modelController = StreamController<EmailSignInModel>();
     Stream<EmailSignInModel> get modelStream => _modelController.stream;
     EmailSignInModel _model = EmailSignInModel();
    
     void dispose() {
       _modelController.close();
     }
    
    void updateWith({
      String email,
      String password,
      EmailSignInFormType formType,
      bool isLoading,
      bool submitted
    
    }) {
      //update model
      _model = _model.copyWith(
        email:email,
        password: password,
        formType: formType,
        isLoading: isLoading,
        submitted: submitted
    
    
      );
      //add updated model _tomodelController
      _modelController.add(_model);
    }
    
    }
    

    Hi, I am new to Flutter and dart and trying to learn bloc in Flutter, I am trying to use BLOC and the also created a model class. My question is What is that copyWith({}) and what it is doing for the email_sign_in_model and for that email_sign_in_bloc? and what is that updateWith doing in the code? Thank you!

  • Milan Poudel
    Milan Poudel almost 4 years
    At the end of return EmailSignInModel(email: email??this.email,.............)........which value does 'this.email' refers to? Does it refer to the first created class, its default values of the constructor at the top?