Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provider: "Bad state: Tried to read a provider that threw during the creation of its value"

I am using the library Provider like this:

runApp(
    LocalizedApp(
      delegate,
      MyApp(
        localizedValues: {},
      ),
    ),
  );

and then:

class MyApp extends StatelessWidget {
  final Map<String, Map<String, String>> localizedValues;
  MyApp({required this.localizedValues});

  @override
  Widget build(BuildContext context) {
    final localizationDelegate = LocalizedApp.of(context).delegate;

    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => MyRents()),
      ],
      child: MaterialApp(
        initialRoute: initialRoute,
    localizationsDelegates: [
      GlobalMaterialLocalizations.delegate,
      GlobalCupertinoLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
      localizationDelegate
    ],

This is my provider:

class MyRents extends ChangeNotifier {
// LIST OF VARS NOT USED AT THIS POINT

  bool loadingUserInfo = false;
  var language;
  // List list = rentsList;
  List list = [];
  int currentPortfolioIndex = 0;
  int currenRentIndex = 0;
  var clonedRent;
  List newGalleryImages = []; // local gallery images that we haven't pushed yet
  SharedPreferences? prefs;
  String viewMode = 'multi';
  bool galleryScrollView = true;
  PageController portfolioController = PageController(initialPage: 0);
  MyUser? user; // TODO: move this to a separate provider
  List<ProductDetails> products = [];
  List<PurchaseDetails> purchases = [];
  bool showAds = false;
  Map companyInfo = {};
  List withholdings = [];
  List<QueryDocumentSnapshot> taxes = [];
// END LIST


  // MyRents(); // TODO: check null (this was working in Flutter 1 but not now so it might be related to the error)...
  final MyRents myRents = MyRents(); // ...and this is the Flutter 2 version

  updateUI() {
    notifyListeners();
  }
}

final MyRents myRents = MyRents();

This was working well in Flutter 1 but I'm getting this error now in Flutter 2:

Reassembling main.dart$main-2339422228391703 failed: ext.flutter.reassemble: (-32000) Server error {"exception":"Bad state: Tried to read a provider that threw during the creation of its value.\nThe exception occurred during the creation of type MyRents.","stack":"#0 _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:661:7)\n#1
_InheritedProviderScopeElement.reassemble (package:provider/src/inherited_provider.dart:367:60)\n#2
Element.reassemble. (package:flutter/src/widgets/framework.dart:3072:13)\n#3
ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)\n#4
Element.reassemble (package:flutter/src/widgets/framework.dart:3071:5)\n#5
Element.reassemble. (package:flutter/src/widgets/framework.dart:3072:13)\n#6
ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)\n#7
Element.reassemble (package:flutter/src/widgets/framework.dart:3071:5)\n#8
Element.reassemble. (package:flutter/src/widgets/framework.dart:3072:13)\n#9
ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)\n#10
Element.reassemble (package:flutter/src/widgets/framework.dart:3071:5)\n#11
Element.reassemble. (package:flutter/src/widgets/framework.dart:3072:13)\n#12
ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)\n#13
Element.reassemble (package:flutter/src/widgets/framework.dart:3071:5)\n#14
Element.reassemble. (package:flutter/src/widgets/framework.dart:3072:13)\n#15
ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)\n#16
Element.reassemble (package:flutter/src/widgets/framework.dart:3071:5)\n#17
Element.reassemble. (package:flutter/src/widgets/framework.dart:3072:13)\n#18
ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)\n#19
Element.reassemble (package:flutter/src/widgets/framework.dart:3071:5)\n#20
StatefulElement.reassemble (package:flutter/src/widgets/framework.dart:4624:11)\n#21
Element.reassemble. (package:flutter/src/widgets/framework.dart:3072:13)\n#22
RenderObjectToWidgetElement.visitChildren (package:flutter/src/widgets/binding.dart:1139:14)\n#23
Element.reassemble (package:flutter/src/widgets/framework.dart:3071:5)\n#24
BuildOwner.reassemble (package:flutter/src/widgets/framework.dart:2892:12)\n#25
WidgetsBinding.performReassemble (package:flutter/src/widgets/binding.dart:951:19)\n#26
BindingBase.lockEvents (package:flutter/src/foundation/binding.dart:298:41)\n#27
BindingBase.reassembleApplication (package:flutter/src/foundation/binding.dart:338:12)\n#28
BindingBase.registerSignalServiceExtension. (package:flutter/src/foundation/binding.dart:375:23)\n#29
BindingBase.registerSignalServiceExtension. (package:flutter/src/foundation/binding.dart:374:17)\n#30
BindingBase.registerServiceExtension. (package:flutter/src/foundation/binding.dart:597:32)\n\n","method":"ext.flutter.reassemble"}

and this other one:

======== Exception caught by Flutter framework ===================================================== The following StateError was thrown during a service extension callback for "ext.flutter.reassemble": Bad state: Tried to read a provider that threw during the creation of its value. The exception occurred during the creation of type MyRents.

like image 330
Dani Avatar asked Mar 29 '21 17:03

Dani


1 Answers

Comment this out

final MyRents myRents = MyRents(); and also use this

ChangeNotifierProvider<MyRents>(create: (_) => MyRents())

like image 105
Huthaifa Muayyad Avatar answered Nov 16 '22 04:11

Huthaifa Muayyad