Bloc: Migration guide to 5.0.0

Created on 3 Jul 2020  路  10Comments  路  Source: felangel/bloc

Is your feature request related to a problem? Please describe.
Migration from 4.0.1 to 5.0.0

Describe the solution you'd like
Provide some steps for different use cases

The specific case is, we have some code like this:

class SampleBloc extends Bloc<SampleEvent, SampleState> {
  String data;

  @override
  SampleState get initialState {
    data = 'initial data';
    if (hasExtraData)
      data += 'extra data';
    return SampleState(data);
  }
}

However, there's no trivial change to migrate that code. We have successfully migrated other initialState where it just returns a state, to provide them to super the constructor of the class.

question

Most helpful comment

Hi @Zazo032 馃憢
Thanks for opening an issue!

I'm working on a formal migration guide and am aiming to have it published tomorrow. In the meantime for your use-case I would recommend the following:

class SampleBloc extends Bloc<SampleEvent, SampleState> {
  SampleBloc({String data = 'initial data', bool hasExtraData})
      : super(SampleState('$data ${hasExtraData ? 'extra data' : ''}'));
}

Let me know if that helps 馃憤

All 10 comments

Hi @Zazo032 馃憢
Thanks for opening an issue!

I'm working on a formal migration guide and am aiming to have it published tomorrow. In the meantime for your use-case I would recommend the following:

class SampleBloc extends Bloc<SampleEvent, SampleState> {
  SampleBloc({String data = 'initial data', bool hasExtraData})
      : super(SampleState('$data ${hasExtraData ? 'extra data' : ''}'));
}

Let me know if that helps 馃憤

Hi @Zazo032 馃憢

Another approach besides the one suggested by @felangel is:

class SampleBloc extends Bloc<SampleEvent, SampleState> {
  final String data;

  SampleBloc(this.data): super((){
    if (hasExtraData)
      data += 'extra data';
    return SampleState(data);
}());
}

Both of those solutions is giving me the following analyzer error:

The instance member 'data' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression.

Note that, in that case, data cannot be passed as a parameter as it's computed within the SampleBloc class.

@Zazo032 can you please provide a sample bloc which more closely represents how your bloc is currently implemented?

@felangel I can privately send you a small real code snippet to the email on your profile, is that ok?

@Zazo032 can you make a private repository and invite me instead?

Done! I uploaded the conflicting file for the migration.

Got the invite but it鈥檚 really late here so I鈥檒l take a look first thing in the morning! 馃槾

Thanks! No hurries 馃槉

@Zazo032 I've created migrated_bloc.dart to illustrate how I would refactor your bloc 馃憤
Also, for more information regarding migrating to v5.0.0 check out the migration guide.

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timtraversy picture timtraversy  路  3Comments

RobPFarley picture RobPFarley  路  3Comments

clicksocial picture clicksocial  路  3Comments

abinvp picture abinvp  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments