Skip to content

Instantly share code, notes, and snippets.

@stevepop
Last active December 8, 2019 22:49
Show Gist options
  • Save stevepop/fc71839701467677399aab329c74db21 to your computer and use it in GitHub Desktop.
Save stevepop/fc71839701467677399aab329c74db21 to your computer and use it in GitHub Desktop.
import 'package:client/providers/ShoppingListProvider.dart';
import 'package:client/screens/Home.dart';
import 'package:client/widgets/StatefulWrapper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:provider/provider.dart';
import 'auth/Auth.dart';
void main() => runApp(MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Auth()),
FutureProvider(create: (_) => ShoppingListProvider().loadShoppingLists(),
),
],
child: MyApp(),
));
class MyApp extends StatelessWidget {
final storage = new FlutterSecureStorage();
@override
Widget build(BuildContext context) {
Future _tryToAuthenticate() async {
String token = await storage.read(key: 'token');
Provider.of<Auth>(context).attempt(token: token);
}
return StatefulWrapper(
onInit: () => _tryToAuthenticate(),
child: MaterialApp(
title: 'Flutter Auth',
theme: ThemeData(
primaryColor: Colors.red[500],
accentColor: Colors.yellow[600],
fontFamily: 'Montserrat',
textTheme: TextTheme(
headline: TextStyle(fontSize: 36.0, fontWeight: FontWeight.bold, color: Colors.red),
title: TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal, fontWeight: FontWeight.w500),
body1: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
),
buttonTheme: ButtonThemeData(
buttonColor: Colors.redAccent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0)
),
textTheme: ButtonTextTheme.primary,
),
),
home: Home(),
),
);
}
}
Error: Could not find the correct Provider<ShoppingListProvider> above this ShoppingList Widget
To fix, please:
* Ensure the Provider<ShoppingListProvider> is an ancestor to this ShoppingList Widget
* Provide types to Provider<ShoppingListProvider>
* Provide types to Consumer<ShoppingListProvider>
* Provide types to Provider.of<ShoppingListProvider>()
* Always use package imports. Ex: `import 'package:my_app/my_code.dart';
* Ensure the correct `context` is being used.
If none of these solutions work, please file a bug at:
https://github.com/rrousselGit/provider/issues
User-created ancestor of the error-causing widget was
MaterialApp
lib/main.dart:32
When the exception was thrown, this was the stack
#0 Provider.of
package:provider/src/provider.dart:264
#1 ShoppingList.build
package:client/…/shopping/ShoppingList.dart:8
#2 StatelessElement.build
package:flutter/…/widgets/framework.dart:4009
#3 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:3941
#4 Element.rebuild
package:flutter/…/widgets/framework.dart:3738
...
import 'dart:convert';
import 'package:client/models/ShoppingListData.dart';
import 'package:dio/dio.dart' as Dio;
import 'package:flutter/material.dart';
import '../dio.dart';
class ShoppingListProvider extends ChangeNotifier {
List<ShoppingListData> shoppingLists;
get allLists => shoppingLists;
get listCount => shoppingLists.length;
Future<List<ShoppingListData>> loadShoppingLists() async {
try {
Dio.Response response = await dio().get('/shopping');
Map<String, dynamic> jsonShoppingList = jsonDecode(response.data);
shoppingLists =
ShoppingList.fromJson(jsonShoppingList['data']).shoppingLists;
return shoppingLists;
} catch (e) {}
return null;
}
}
import 'package:client/providers/ShoppingListProvider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class ShoppingList extends StatelessWidget {
@override
Widget build(BuildContext context) {
var _shoppingLists = Provider.of<ShoppingListProvider>(context).allLists;
print(_shoppingLists);
return Scaffold(
appBar: AppBar(title: Text('LetsShop - Your Shopping Lists')),
body: Column(
children: <Widget>[
Expanded(
child: _shoppingLists == null ? Text("No products lists found") : ListView.builder(
itemCount: _shoppingLists.length,
itemBuilder: (context, index) {
return Container(
child: Center(
child: Text('${_shoppingLists[index].name}'),
),
);
}),
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment