Created
September 16, 2019 05:43
-
-
Save ponnamkarthik/f7aefd6d89c502f27daac594fb7723da to your computer and use it in GitHub Desktop.
Flutter Bloc Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) Foodie Labs, Inc - All Rights Reserved | |
* | |
* Unauthorized copying of this file, via any medium is strictly prohibited | |
* Proprietary and confidential | |
* Written by Karthik Ponnam for Foodie Labs, Inc , April 2019. | |
*/ | |
// Generic Interface for all BLoCs | |
import 'package:flutter/material.dart'; | |
abstract class BlocBase { | |
void dispose(); | |
} | |
// Generic BLoC provider | |
class BlocProvider<T extends BlocBase> extends StatefulWidget { | |
BlocProvider({ | |
Key key, | |
@required this.child, | |
@required this.bloc, | |
}) : super(key: key); | |
final T bloc; | |
final Widget child; | |
@override | |
_BlocProviderState<T> createState() => _BlocProviderState<T>(); | |
static T of<T extends BlocBase>(BuildContext context) { | |
final type = _typeOf<BlocProvider<T>>(); | |
BlocProvider<T> provider = context.ancestorWidgetOfExactType(type); | |
return provider.bloc; | |
} | |
static Type _typeOf<T>() => T; | |
} | |
class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> { | |
@override | |
void dispose() { | |
widget?.bloc?.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return widget.child; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi ,context.ancestorWidgetOfExactType(type); is deprecated with the new version on flutter how fix that now ?