GithubHelp home page GithubHelp logo

thewckd / blocfromzerotohero Goto Github PK

View Code? Open in Web Editor NEW
586.0 586.0 299.0 81 KB

This repo contains the code I wrote in my BLoC - From Zero to Hero Tutorial Series on my Flutterly - YouTube Channel

Home Page: https://www.youtube.com/c/flutterly

Kotlin 0.79% Swift 2.32% Objective-C 0.22% Dart 96.67%

blocfromzerotohero's Introduction

T's GitHub stats

Hello there πŸ‘‹

  • πŸ”­ I’m currently working on developing high-performance crypto trading bots using Python on Binance Exchange.
  • 🌱 I’m currently learning more about advanced data science techniques for financial markets.
  • πŸ‘― I’m looking to collaborate on open-source projects related to algorithmic trading or financial data analysis.
  • πŸ€” I’m looking for help with optimizing trading strategies for efficiency and profitability.
  • πŸ’¬ Ask me about Python programming, data analysis with pandas, or deploying applications on AWS and Hetzner.
  • πŸ“« How to reach me: LinkedIn or YouTube
  • ⚑ Fun fact: I enjoy exploring new hiking trails and travelling, check out my trip to the Faroe Islands

blocfromzerotohero's People

Contributors

raychencode avatar thewckd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blocfromzerotohero's Issues

Trying to run the chapter 2 code gives error

Improperly formatted define flag: Blocs

FAILURE: Build failed with an exception.

  • Where:
    Script '/Users/a35850/development/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1035

  • What went wrong:
    Execution failed for task ':app:compileFlutterBuildDebug'.

Process 'command '/Users/a35850/development/flutter/bin/flutter'' finished with non-zero exit value 1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1s
Exception: Gradle task assembleDebug failed with exit code 1

Local BLoc access actually works.

I am following your tutorial and for some reason when I am just pushing the second screen , it does not crash and works as intended. I am using bloc 8.1 and Flutter 3.10. Has there been an update that now supports Flutter anonymous routes or now the two pages share a context?

[Part 5] Running the test fails with null safety enabled

I am trying to run the code with null safety enabled in the project. I keep getting the following error when I run the test on counter_cubit_test.dart:

Null check operator used on a null value

This is the code in the counter_cubit.dart file:

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

part 'counter_state.dart';

class CounterCubit extends Cubit<CounterState> {
  CounterCubit() : super(CounterState(counterValue: 0));

  void increment() => emit(
      CounterState(counterValue: state.counterValue! + 1, wasIncremented: true));

  void decrement() => emit(CounterState(
      counterValue: state.counterValue! - 1, wasIncremented: false));
}

This is the code in the counter_state.dart file:

part of 'counter_cubit.dart';

class CounterState extends Equatable {
  final int? counterValue;
  final bool? wasIncremented;

  CounterState({required this.counterValue, this.wasIncremented});

  @override
  List<Object> get props => [this.counterValue!, this.wasIncremented!];
}

This is the code in the main.dart file:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_counter_bloc/cubits/counter_cubit.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Demo",
      theme: ThemeData(
        primaryColor: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: BlocProvider<CounterCubit>(
        create: (context) => CounterCubit(),
        child: MyHomePage(title: "Flutter Demo Home Page"),
      ),
    );
  }
}

///Used a stateless widget since state is handled by the Bloc library in this case. I referred the
///following link: https://stackoverflow.com/questions/58864869/flutter-state-management-bloc-stateless-vs-stateful-widget
class MyHomePage extends StatelessWidget {
  final String? title;

  const MyHomePage({Key? key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title!),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("You have pushed this button this many times:"),
            BlocConsumer<CounterCubit, CounterState>(
              ///Refactored the if...else block into a function to show the SnackBar Widget
                listener: (context, state) => snackBarFunction(state.wasIncremented!, context),
                ///Refactored the if...else block into a function that returns a Text widget
                builder: (context, state) => counterText(state.counterValue!, context)),
            SizedBox(height: 24),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                FloatingActionButton(
                  onPressed: () => BlocProvider.of<CounterCubit>(context).decrement(),
                  heroTag: Text("$title"),
                  tooltip: "Decrement",
                  child: Icon(Icons.remove),
                ),
                FloatingActionButton(
                  onPressed: () => BlocProvider.of<CounterCubit>(context).increment(),
                  heroTag: Text("$title #2"),
                  tooltip: "Increment",
                  child: Icon(Icons.add),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

///This function is used to show the snack bar widget depending on whether the counter was incremented
///or decremented
void snackBarFunction(bool counterState, context) {
  if (counterState == true) {
    ///Scaffold.of(context).showSnackBar(snackBar) is deprecated
    ///Using ScaffoldMessenger.of(context).showSnackBar(snackBar) instead
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text("Incremented!"),
      duration: Duration(milliseconds: 300),
    ));
  } else {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text("Decremented!"),
      duration: Duration(milliseconds: 300),
    ));
  }
}

///This function is used to change the returned Text widget in accordance with the value of the counter
Text counterText(int counterValue, context) {
  if (counterValue < 0) {
    return Text("BRR, Negative $counterValue",
        style: Theme.of(context).textTheme.headline4);
  } else if (counterValue % 2 == 0) {
    return Text("YAAAY $counterValue",
        style: Theme.of(context).textTheme.headline4);
  } else if (counterValue == 5) {
    return Text("HMM, NUMBER 5", style: Theme.of(context).textTheme.headline4);
  } else {
    return Text("$counterValue",
        style: Theme.of(context).textTheme.headline4);
  }
}

This is the code in the counter_cubit_test.dart file:

import 'package:flutter_counter_bloc/cubits/counter_cubit.dart';
import 'package:test/test.dart';
import 'package:bloc_test/bloc_test.dart';

void main() {
  group("CounterCubit", (){

    CounterCubit? counterCubit;

    setUp((){
      counterCubit = CounterCubit();
    });
    tearDown((){
      counterCubit!.close();
    });

    test("Initial state of CounterCubit is CounterState(counterValue: 0)", () {
      expect(counterCubit!.state, CounterState(counterValue: 0));
    });

    blocTest<CounterCubit, CounterState>(
      'the CounterCubit should emit a CounterState(counterValue:1, wasIncremented:true) when the increment function is called',
      build: () => counterCubit!,
      act: (cubit) => cubit.increment(),
      expect: () => <CounterState>[CounterState(counterValue: 1, wasIncremented: true)],
    );

    blocTest<CounterCubit, CounterState>(
      'the CounterCubit should emit a CounterState(counterValue:-1, wasIncremented:false) when the decrement function is called',
      build: () => counterCubit!,
      act: (cubit) => cubit.decrement(),
      expect: () => <CounterState>[CounterState(counterValue: -1, wasIncremented: false)],
    );
  });
}

[Part 3]

image

CounterState({@required this.counterValue, this.wasIncremented});

this shows an error and it works fine when i remove '@' from required, it also asks to add required to 'this.wasIncremented' .
means CounterState({required this.counterValue, required this.wasIncremented}); works fine ,
but in counter_cubit it asks to pass the required parameter of 'this.wasIncremented' in countercubit()

Questions about Bloc Provider using Route access method

Hi WCKD,
I use route access method to locally provide bloc just like how you do in the youtube tutorial. However the situation is a bit weird. The thing is that after exception the program does not shutdown and the widget depending on the bloc(DatesBloc) seems to work correctly, which means it finds the bloc and give the correct responses to the widget just causing exception earlier. What's more, if I provide the bloc in homepage wrapping DatesPage instead of providing it by route, the exception is just gone. And, if I exchange the order of DatesPage and ChatsPage in HomePage, it turns out to be that it is ChatsBloc causes the exception instead of DatesBloc. Is there any possible reason to this phenomenon or any possible solution to resolve this issue?
ζˆͺεœ– 2021-03-26 δΈ‹εˆ12 58 40
ζˆͺεœ– 2021-03-26 δΈ‹εˆ1 07 33

ζˆͺεœ– 2021-03-26 δΈ‹εˆ1 00 26
ζˆͺεœ– 2021-03-26 δΈ‹εˆ1 01 07

Always throws UnimplementedError

With current Dart (2.12.2 - I don't know how it worked in the past) this line is always called as break only jumps out of switch, not a method. Because of that, there is always an error returned and printed into the console:

D:\SDK\Flutter\flutter\bin\cache\dart-sdk\bin\dart.exe --enable-asserts D:\Projects\Tutorials\Flutter\blockhero\tryout\lib\main-bloc.dart
lib/main-bloc.dart: Warning: Interpreting this as package URI, 'package:streams/main-bloc.dart'.
1
Unhandled exception:
Unhandled error UnimplementedError occurred in Instance of 'CounterBloc'.
#0 CounterBloc.mapEventToState (package:streams/main-bloc.dart:18:5)

#0 BlocBase.onError. (package:bloc/src/bloc.dart:389:7)
#1 BlocBase.onError (package:bloc/src/bloc.dart:390:6)
#2 _RootZone.runBinaryGuarded (dart:async/zone.dart:1558:10)
#3 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#4 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:16)
#5 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:280:7)
#6 _SyncBroadcastStreamController._sendError. (dart:async/broadcast_stream_controller.dart:393:20)
#7 _BroadcastStreamController._forEachListener (dart:async/broadcast_stream_controller.dart:323:15)
#8 _SyncBroadcastStreamController._sendError (dart:async/broadcast_stream_controller.dart:392:5)
#9 _BroadcastStreamController._addError (dart:async/broadcast_stream_controller.dart:290:5)
#10 _RootZone.runBinaryGuarded (dart:async/zone.dart:1558:10)
#11 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#12 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:16)
#13 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:280:7)
#14 _ForwardingStreamSubscription._addError (dart:async/stream_pipe.dart:128:11)
#15 _ForwardingStream._handleError (dart:async/stream_pipe.dart:95:10)
#16 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:157:13)
#17 _RootZone.runBinaryGuarded (dart:async/zone.dart:1558:10)
#18 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#19 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:16)
#20 _DelayedError.perform (dart:async/stream_impl.dart:602:14)
#21 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
#22 _PendingEvents.schedule. (dart:async/stream_impl.dart:663:7)
#23 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#24 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#25 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:120:13)
#26 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:185:5)

Process finished with exit code 255

part #6 - snackbar showing up multi times

if navigate to second or third screen an press the increase or decrease button snackbar showing up one time and when you back to the previous screen snakbar showing up again same number of latest screen.

CHAPTER 5.1 BLoC Testing Implementation (1:19:00 - 1:21:00)

@flutterly, awesome course so far but i seem to be stuck at (1:21:00) my error is

The argument type 'List' can't be assigned to the parameter type 'dynamic Function()'. [29,17]
The argument type 'List' can't be assigned to the parameter type 'dynamic Function()'. [35,17]
so i went on to run the test and it FAILED... :'(

please could you or someone facing the same error/ OVERCAME this error kindly assist... thanks! :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.