Flutter BLoC Library Tutorial - Simple BLoC Pattern Solution

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ธ.ค. 2024

ความคิดเห็น • 90

  • @ResoCoder
    @ResoCoder  4 ปีที่แล้ว +5

    🚨 Check out the tutorial covering the newest version of the Bloc package:
    th-cam.com/video/y564ETOCog8/w-d-xo.html

    • @monikasharma3403
      @monikasharma3403 3 ปีที่แล้ว

      hi Reso Coder, nice tutorial, but i. have some doubt when i create CounterState._() its gives me an error Non-nullable instance field 'counter' must be initialized. I am using bloc library. with version 7.0.0. please help me with this.

  • @AbdulmalikHamid
    @AbdulmalikHamid 6 ปีที่แล้ว +24

    This is the best bloc tutorial EVER!

  • @ResoCoder
    @ResoCoder  6 ปีที่แล้ว +9

    If you want to follow this tutorial, either use the library version 0.4.12 or read on! *Check out the replies for a solution*
    There is one breaking change in 0.5.0 and onwards. Duplicate states are no longer emitted by the bloc. If you want to use the new version, you have to override the == operator, and also not edit currentState directly when incrementing, but rather make a copy of the object. In other words, make CounterState an immutable class.
    If you know how to do this, cool. If not, just use the older version and wait for another tutorial where we will use the built_value library which can create data classes which are immutable and nice to work with.

    • @gmc254quads6
      @gmc254quads6 6 ปีที่แล้ว

      Thanks. I should have read this before trying it out. I was almost going nuts trying to do a similar thing to the point I cloned your app from Github and it worked. Then I changed the bloc version and it broke down leading me to the comments. Looking forward to your next tutorial on this.

    • @ilirvasili8847
      @ilirvasili8847 6 ปีที่แล้ว +3

      Simple changes I made for it to work with the latest bloc:
      1.
      factory CounterState.initial(int times) {
      return CounterState._()..counter = times;
      }
      2.
      CounterState get initialState => CounterState.initial(0);
      3.
      on mapEventToState() replace
      yield currentState..counter += 1;
      yield currentState..counter -= 1;
      respectively with
      yield CounterState.initial(currentState.counter + 1);
      yield CounterState.initial(currentState.counter - 1);
      Thanks

    • @chinyun1526
      @chinyun1526 5 ปีที่แล้ว

      Yeah, I think this video is not that helpful, now. I learned from this and stuck for hours because the view not update with same state (same instance). You should make your comment stick on top to prevent someone stucking.

  • @JmGodlike
    @JmGodlike 6 ปีที่แล้ว +16

    Thanks for the video. Was really helpful!
    Would be grateful if you can maybe do an end-to-end complex app implementation of the BLoC library, with repository call to API call and local db retrieval. The doc skipped those part for simplicity. Looking forward to that, thanks again!

    • @aislanarislou
      @aislanarislou 5 ปีที่แล้ว +1

      Local db is something I'm looking for as well.

  • @SakshatShinde
    @SakshatShinde 6 ปีที่แล้ว +8

    Would you be making a complete flutter tutorial from the beginner level to advanced? Your teaching style is great!

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +5

      Yes, although currently I have only an intermediate tutorial planned.

    • @SakshatShinde
      @SakshatShinde 6 ปีที่แล้ว +1

      @@ResoCoder Sure, maybe in future I guess!
      Great work though!

  • @oztech
    @oztech 5 ปีที่แล้ว +4

    This BLOC tutorial is the best i've seen so far in TH-cam! however, async* and yield was never seen before and still not understanding.

    • @codeityourself8064
      @codeityourself8064 3 ปีที่แล้ว

      Maybe this is a late answer, but when you use the yield function, for a better understanding, it will return a list. By example, if you use yield inside a for loop of 100 iterations, it will return a list of 100 items, and in Flutter, async* is linked to it (as long as I know)

  • @wexywexywex
    @wexywexywex 4 ปีที่แล้ว +2

    Hello. I just followed this tutorial and things seems to be totally out of sync with bloc_provider now. did the updates to bloc_provider really change how code is written in the last few years? if so have you made a new video showing how to use bloc_providers more recent api?

  • @filipsavic2165
    @filipsavic2165 4 ปีที่แล้ว +4

    For anyone doing this at flutter_bloc version 4.0.0, the counter_bloc.dart mapEventToState() method should look like this:
    @override
    Stream mapEventToState(CounterEvent event) async* {
    if (event is IncrementEvent) {
    yield state..counter += 1;
    } else if (event is DecrementEvent) {
    yield state..counter -= 1;
    }
    }
    (No need to pass 'CounterState currentState' to the function.)

    • @henryborja702
      @henryborja702 4 ปีที่แล้ว +2

      Thanks a lot, you saved my life

    • @Koustav7
      @Koustav7 4 ปีที่แล้ว +1

      thanks a lot

    • @filipsavic2165
      @filipsavic2165 4 ปีที่แล้ว

      You are welcome :D

  • @김주혁-s6g
    @김주혁-s6g 5 ปีที่แล้ว

    Really great bloc tutorial.

  • @sephyshen2510
    @sephyshen2510 5 ปีที่แล้ว

    Very helpful. Thanks for the video. Looking forward to your next tutorial.

  • @MarciMatuz
    @MarciMatuz 5 ปีที่แล้ว +2

    I love BLOC pattern but this package is like ScopedModel package.
    Basicly if your state object has multiple properties and if you use multiple BlocBuilder with same bloc instance and whenever you dispatch an event then all BlocBuilders builder method rerun even if some of them not intrested in the changed property. So better use StreamBuilder with Streams instead of you get the whole state as parameter in build method.

  • @38sabbath38
    @38sabbath38 5 ปีที่แล้ว

    Thanks a lot! Great tutorial

  • @latifamuhammadi7663
    @latifamuhammadi7663 4 ปีที่แล้ว

    You are awesome sir
    Please do more videos on real app using apis with Bloc

  • @govingupta5065
    @govingupta5065 5 ปีที่แล้ว

    Please explain in detail about bloc builder ,bloc provider, bloc listner in which case what we should use this all ...BTW great tuts..

  • @ibraheemdev2290
    @ibraheemdev2290 5 ปีที่แล้ว

    Great and helpful tutorial, thanks

  • @theolm
    @theolm 6 ปีที่แล้ว +2

    Great tutorial.
    Please make a full app in flutter, like the weather app. =D

  • @franz5944
    @franz5944 5 ปีที่แล้ว

    Really helpful. Thank you.

  • @nishantdesai3705
    @nishantdesai3705 6 ปีที่แล้ว +2

    Hi, how can we use flutter_bloc when a widget depends on more than one blocs ?
    for example I have a widget w1 which depends on data from remote server (bloc1) and a bloc which responds to internet connectivity (bloc2), as all pages (including the one which has widget w1) of my app responds to change in internet connectivity I decided to use bloc for that too, now how can I use both of these blocs (viz bloc1 and bloc2) to render widget w1 ? Do i have to use nested bloc providers ? or is there a better way ?

    • @aislanarislou
      @aislanarislou 5 ปีที่แล้ว

      For what I've seen in another tutorial from documentation, you can use nested blocs.

  • @MrLarryQ
    @MrLarryQ 5 ปีที่แล้ว

    Your tutorials are fantastic, however I think this one went a bit too quick toward the end, particularly the BlocProvider.of stuff. I know what you're doing but that's because I've used Blocs before. Someone new to them, and to the flutter_bloc library, who hasn't seen it before would probably be confused by the last 5-7 minutes. May I suggest a separate video that goes a bit more detail into how to use the flutter_bloc library and all the BlocProvider.of code?

  • @Dumveloper
    @Dumveloper 5 ปีที่แล้ว

    why i got a error when I put CounterEvent parameter on mapEventToState. there is error on @override, it says "'CounterBloc.mapEventToState' ('Stream Function(CounterState, CounterEvent)') isn't a valid override of 'Bloc.mapEventToState' ('Stream Function(CounterEvent)').dart(invalid_override)"

  • @guybashan9897
    @guybashan9897 5 ปีที่แล้ว

    Thanks for the GREAT video. Not sure I quite understand the last part with the BlocProvider. For example, if I have method creating some Widget, I will have to pass to it as a parameter the context (in order to use the BlocProvider). So what is the difference between passing the context or the BLOC as a parameter?

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว

      I'm glad you liked the video, Guy! The difference is that you can get the Bloc from the provider even down the widget tree (from a different class).
      Let's say you have a HomePage widget which contains a UserListView widget to keep the code clean. With BlocProvider, you can easily access the Bloc instantiated in the HomePage inside the UserListView.
      I hope this makes sense, check out some of my newer Bloc tutorials. Things have changed a bit since this video was published.

  • @raj-f6b4z
    @raj-f6b4z 15 วันที่ผ่านมา

    Great 👍

  •  6 ปีที่แล้ว

    What you need multiple blocs? Let's say you need one bloc to manage all the states and another one for managing a database logic, or the state of a user authentication? thanks

    •  6 ปีที่แล้ว

      I think I found my answer, I see there is a BlocDelegate on the library which basically "is a singleton which oversees all Blocs and delegates responsibilities to the BlocDelegate." Is that how you would solve it? I don't like the usage of Singleton.

  • @stijnmathysen7453
    @stijnmathysen7453 6 ปีที่แล้ว +1

    Another great tutorial!
    Just a thought; to clean up your onPressed: anonymous functions, you could change;
    onPressed: () => BlocProvider.of(context).onIncrement(),
    to
    onPressed: BlocProvider.of(context).onIncrement,
    Also, while following allong I found it better to stick with flutter_bloc: ^0.4.12 instead of the latest version ^0.5.0 which was recently released. I just can't seem to get it to work with the latest version?

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว

      Oh, I hope there are no breaking changes in 0.5.0!

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +1

      Actually, I found out that there is one breaking change. Duplicate states are no longer emitted by the bloc. If you want to use the new version, you have to override == operator, and also not edit currentState directly when incrementing, but rather make a copy of the object. In other words, make CounterState an immutable class.

  • @michalrv3066
    @michalrv3066 5 ปีที่แล้ว

    Any reason why you used Classes for events instead of Enums like shown in docs? Anyways, I'm wondering what would be the best way of passing data to bloc, like product ID received from previous screen so it can go ahead and fetch the necessary data :/

  • @miabellasalonandmedicalspa2822
    @miabellasalonandmedicalspa2822 5 ปีที่แล้ว +1

    sounds like redux renamed so code grows fast?

  • @MattDuarte11
    @MattDuarte11 5 ปีที่แล้ว

    This is like redux for React. Only with classes. Also I am new to Flutter just started. Is there a way to connect my app to a DB? Like PostgreSQL or mongo? Basically where does the backend come in?

  • @sadekhossain9566
    @sadekhossain9566 5 ปีที่แล้ว

    Hi, i am curious how you will use/call a different bloc lets say updateUserBloc that doesn't exists in counter bloc and you want to use properties from both bloc in the the same widget? How you will use from two or more different blocs?

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว +1

      In addition to BlocProvider there is also a BlocProviderTree which lets you specify a list of Blocs.

    • @sadekhossain9566
      @sadekhossain9566 5 ปีที่แล้ว

      Is there any documentation page for all these? Many thanks

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว +1

      Of course! felangel.github.io/bloc/#/

  • @zehijean8817
    @zehijean8817 5 ปีที่แล้ว

    Great explaining...would you mind showing a more elaborate exemple with two blocks communicating together alongside with dealing with resources fetched from the network(DB,API) great stuff

  • @leukongsun4845
    @leukongsun4845 5 ปีที่แล้ว

    What is cascading operator?

  • @임창수-c7c
    @임창수-c7c 6 ปีที่แล้ว

    Awesome! How about an example app with basic auth + other features. Thank you!

  • @walidbagh
    @walidbagh 6 ปีที่แล้ว

    Nice follow-up :D

  • @tribeworld5360
    @tribeworld5360 4 ปีที่แล้ว

    Why does the constructor have ._() ? and why does the factory constructor return ._()..counter

  • @creative-commons-videos
    @creative-commons-videos 5 ปีที่แล้ว

    Validation please, i am not able to integrate validation part for flutter_bloc library

  • @TheVikke2
    @TheVikke2 6 ปีที่แล้ว

    Hey, I had a weird bug/glitch where BlocBuilder did not update the screen. I thought I had made a mistake in the code, but everything was as in the example. Took me hours to work it out but after I reran *flutter packages get* it started to work. Was weird because the bloc worked and I got print statements but the screen didn't change. Any idea what caused this?

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว

      No idea, I'm glad you got it solved.

  • @LuisOtavio-dt8nu
    @LuisOtavio-dt8nu 5 ปีที่แล้ว

    Nice Tutorial, but i had a problem when i ran it: "Error: No named parameter with the name 'seedValue'. "
    I fixed it by replacing the version flutter_bloc: ^0.4.12 to flutter_bloc: ^0.8.0 .

  • @Daaab89
    @Daaab89 6 ปีที่แล้ว +1

    very good tutorial. Short yet straight to the point.
    I've got one question: What if you want your bloc only to listen to database changes (or for example Firestore data changes), and you only need stream with no sink (no events passed - you only listen to data ) - is it possible with this library ?
    Also I'm a little bit confused about mapEventToState. First of all you have to map Event not to State but to Stream (of States). And the second thing is (please correct me if Im wrong) that I thought that you should subscribe to the stream of States, and emit new state when it changes, yet here you have to return new Stream each time State changes - I dont see any difference between returning Stream of States each time State changes, and returning State itself.

    • @ThinkDigitalRepair
      @ThinkDigitalRepair 5 ปีที่แล้ว

      The library doesn't stop you from doing anything. So yes, any questions you ask about if it's possible, the answer is yes. It's just a pattern. What you can do is add a listener to the API and onData, bloc.dispatch(myApiEvent()) and set that off on the side somewhere in a function that you call on an initState.

  • @NelZFS
    @NelZFS 5 ปีที่แล้ว

    very good!

  • @SAlmanKhanDev
    @SAlmanKhanDev 5 ปีที่แล้ว

    Hey, thanks for the nice tutorial. I request you to create one video covering redux! Thanks again!

  • @furkanvatandas4819
    @furkanvatandas4819 5 ปีที่แล้ว

    Awesomee thanks

  • @hemlock7564
    @hemlock7564 3 ปีที่แล้ว

    Bro I'm a beginner at this flutter thing am an android dev can you please upload beginner level tutorials it'll be very helpful ❤️

  • @mostasimbillah
    @mostasimbillah 6 ปีที่แล้ว

    when you will be publish android mvvm tutorial next part?

  • @vrushangdesai2813
    @vrushangdesai2813 4 ปีที่แล้ว

    Bro pls add one video using multibloc

  • @piyushdongre325
    @piyushdongre325 4 ปีที่แล้ว

    Watch previous video in the series to get your concepts clear

  • @Spacewalker3043
    @Spacewalker3043 4 ปีที่แล้ว

    BLoC makes me
    🤯

  • @anirudhamahale8715
    @anirudhamahale8715 5 ปีที่แล้ว +1

    I did exactly what you did still my UI didn't update. Then I just copied your class ContainerWidget and it worked😂 But anyways the tutorial was 🔥

    • @arifikhsanudin9724
      @arifikhsanudin9724 5 ปีที่แล้ว

      same with me. just writing the same code doesnt work. but after copied, everything just fine

  • @andreykaok9497
    @andreykaok9497 5 ปีที่แล้ว +1

    Little (but important) changes ;) to be working with flutter_bloc: ^0.5.4 ^0.6.0 etc.
    gist.github.com/ruan65/41d1aa37402e49eea065c5bbe0698853

    • @kuestua
      @kuestua 5 ปีที่แล้ว

      thanks! this worked!

  • @jacintoJoao
    @jacintoJoao 5 ปีที่แล้ว +1

    this is for any dummy person like me> the new version
    flutter_bloc: ^0.21.0 above
    Make changes for the following lines:
    return BlocProvider(
    builder: _counterBloc,
    child: CounterWidget(),
    );
    to
    return BlocProvider(
    builder: (BuildContext context) => CounterBloc(),
    child: CounterWidget(),
    );

  • @simransingh1507
    @simransingh1507 6 ปีที่แล้ว

    Great Work. I think it would be better if you create flutter tutorials in Android Studio. It has great support with the latest version. :)

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +6

      I feel like Android Studio is too bloated for what it is. Flutter doesn't need a heavy IDE, at least in my experience - and I'm coming from native Android development so I made a conscious switch to VS Code.

    • @aislanarislou
      @aislanarislou 5 ปีที่แล้ว

      @@ResoCoder I agree!! Android Studio is bloated as hell... and heavy.

  • @sajad2126
    @sajad2126 4 ปีที่แล้ว

    now it is on version flutter_bloc: ^4.0.0 please make new video with new version . thank you

  • @simonhutton2497
    @simonhutton2497 5 ปีที่แล้ว

    Unfortunately this tutorial is out of date and shouldn't really be here any more. The updated tutorial (referenced in the pinned comment above) uses a totally different use case and so doesn't relate to the first tutorial in the series any longer (which used the flutter default counter app). So all in all this series no longer flows together :-(

  • @vasilvasilich8224
    @vasilvasilich8224 4 ปีที่แล้ว

    You can try this library with generic approach for cooking graphql_flutter + bloc
    pub.dev/packages/graphql_flutter_bloc

  • @millerliang3565
    @millerliang3565 5 ปีที่แล้ว

    WHERE IS THE BLOCBUILDER WIDGET!!!!!!

  • @chrisik100
    @chrisik100 5 ปีที่แล้ว +1

    Sooo fu*king hard!

  • @mohammadhosseinramezanzade9675
    @mohammadhosseinramezanzade9675 5 ปีที่แล้ว

    you used an old version of Bloc.

  • @bazoozoo1186
    @bazoozoo1186 5 ปีที่แล้ว +2

    frankly, I hate it.
    it looks so over-complicated... and that Bloc object sending events to itself... yuck!
    scoped_model framework looks much less cluttered.

  • @thatsenam9183
    @thatsenam9183 4 ปีที่แล้ว

    Its complicated

  • @edwardnjoroge5222
    @edwardnjoroge5222 5 ปีที่แล้ว

    Play at 1.75 speed. You're welcome.

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว +1

      I watch everything at 2x

  • @AbdulmalikHamid
    @AbdulmalikHamid 6 ปีที่แล้ว

    This is the best bloc tutorial EVER!