felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.8k stars 3.39k forks source link

BlocProvider without a child error: A build function returned null. #950

Closed caneva20 closed 4 years ago

caneva20 commented 4 years ago

Hi, I'm new to this library and thus it might not be a bug, but I think it's worth sharing at least :)

Describe the bug If you have a BlocProvider<SomeBloc>() without the child parameter set you'll get a pretty "obscure" error without any hint to what is wrong about your code. Having a BlocProvider without a child might not the correct usage of a BlocProvider, but there's no @required for the child parameter nor an assert(child != null)

To Reproduce Steps to reproduce the behavior:

  1. Create a BlocProvider<SomeBloc>() with only the create parameter set.
  2. Hot restart the app
  3. See error

Expected behavior an @required attribute/annotation so that we can at least have a nice warning in the IDE or an assert(child != null) telling us what's wrong

Logs Run flutter analyze and attach any output of that command below. If there are any analysis errors, try resolving them before filing this issue.

info - Unused import: 'package:intl/intl.dart' - lib\locales\messages.dart:1:8 - unused_import info - Unused import: 'package:rxdart/rxdart.dart' - lib\login\bloc\login_bloc.dart:10:8 - unused_import info - Unused import: 'package:rxdart/rxdart.dart' - lib\register\bloc\register_bloc.dart:10:8 - unused_import info - Dead code - lib\screens\home_screen.dart:52:5 - dead_code warning - The parameter 'userRepository' is required. - test\widget_test.dart:16:29 - missing_required_param

Paste the output of running flutter doctor -v here. F:\Dev\Flutter\exponential_journey>flutter doctor -v [√] Flutter (Channel stable, v1.12.13+hotfix.8, on Microsoft Windows [Version 10.0.18362.356], locale pt-BR) • Flutter version 1.12.13+hotfix.8 at F:\flutter • Framework revision 0b8abb4724 (4 weeks ago), 2020-02-11 11:44:36 -0800 • Engine revision e1e6ced81d • Dart version 2.7.0

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2) • Android SDK at C:\Users\Jesus\AppData\Local\Android\sdk • Android NDK location not configured (optional; useful for native profiling support) • Platform android-29, build-tools 29.0.2 • Java binary at: F:\IDEs\apps\AndroidStudio\ch-0\192.6241897\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04) • All Android licenses accepted.

[!] Android Studio (version 3.6) • Android Studio at F:\IDEs\apps\AndroidStudio\ch-0\192.6241897 X Flutter plugin not installed; this adds Flutter specific functionality. X Dart plugin not installed; this adds Dart specific functionality. • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[√] IntelliJ IDEA Ultimate Edition (version 2019.3) • IntelliJ at F:\IDEs\apps\IDEA-U\ch-0\193.6494.35 • Flutter plugin version 42.1.4 • Dart plugin version 193.5731

[√] VS Code (version 1.43.0) • VS Code at C:\Users\Jesus\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.8.1

[√] Connected device (1 available) • AOSP on IA Emulator • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)

! Doctor found issues in 1 category.

Additional context Add any other context about the problem here.

felangel commented 4 years ago

Hi @caneva20 👋 Thanks for opening an issue!

Sorry for the inconvenience but that is how BlocProvider works because it can be used within MultiBlocProvider without a child. I will try to improve the exception to make it more clear 👍

felangel commented 4 years ago

Depends on https://github.com/rrousselGit/provider/issues/401 PR: https://github.com/rrousselGit/provider/pull/402

felangel commented 4 years ago

This was fixed in provider and included in v4.0.5 🎉