Expected ';' after this. , stthrow ProviderNotFoundException(T, context.widget.runtimeType)

Issue

Q. I implemented MultiProvider in the manner shown below in my MAIN file. While running I am thrown the below error. Can anybody help me with this?
Thank you.

#Error

../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:7: Error: Expected an identifier, but got ‘,’.
Try inserting an identifier before ‘,’.
, stthrow ProviderNotFoundException(T, context.widget.runtimeType);
^
../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:357:49: Error: Expected ‘;’ after this.
if (inheritedElement == null && null is! T) {
^
../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:7: Error: Unexpected token ‘,’.
, stthrow ProviderNotFoundException(T, context.widget.runtimeType);
^
../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:9: Error: ‘stthrow’ isn’t a type.
, stthrow ProviderNotFoundException(T, context.widget.runtimeType);
^^^^^^^
../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:17: Error: Expected ‘;’ after this.
, stthrow ProviderNotFoundException(T, context.widget.runtimeType);
^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:44: Error: Expected ‘)’ before this.
, stthrow ProviderNotFoundException(T, context.widget.runtimeType);
^

FAILURE: Build failed with an exception.

  • Where:
    Script ‘C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle’ line: 1070

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

Process ‘command ‘C:\src\flutter\bin\flutter.bat” 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 22s
Running Gradle task ‘assembleDebug’… 24.4s
Exception: Gradle task assembleDebug failed with exit code 1


#My main file

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:e_shop/Config/config.dart';
import 'package:e_shop/Counters/changeAddress.dart';
import 'package:e_shop/Counters/itemQuantity.dart';
import 'package:e_shop/Counters/totalAmount.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'Config/config.dart';
import 'Counters/cartItemCounter.dart';
import 'splash/splashscreen.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  EcommerceApp.auth = FirebaseAuth.instance;
  EcommerceApp.sharedPreferences = await SharedPreferences.getInstance();
  EcommerceApp.firestore = FirebaseFirestore.instance;

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => CartItemCounter()),
        ChangeNotifierProvider(create: (context) => ItemQuantityCounter()),
        ChangeNotifierProvider(create: (context) => AddressChangeCounter()),
        ChangeNotifierProvider(create: (context) => TotalAmountCounter()),
        //OR -------
        // ChangeNotifierProvider.value(value: CartItemCounter()),
        // ChangeNotifierProvider.value(value: ItemQuantityCounter()),
        // ChangeNotifierProvider.value(value: AddressChangeCounter()),
        // ChangeNotifierProvider.value(value: TotalAmountCounter()),
      ],
      child: MaterialApp(
          title: 'e-Shop',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primaryColor: Colors.green,
          ),
          home: SplashScreen()),
    );
  }
}

#My Counter dart files (4 files almost similar to below file)

import 'package:flutter/foundation.dart';

class CartItemCounter with ChangeNotifier {
  int _counter = EcommerceApp.sharedPreferences!
          .getStringList(EcommerceApp.userCartList)!
          .length -
      1;

  int get count => _counter;

  Future<void> displayResult() async {
    int _counter = EcommerceApp.sharedPreferences!
            .getStringList(EcommerceApp.userCartList)!
            .length -
        1;

    await Future.delayed(Duration(milliseconds: 100), () {
      notifyListeners();
    });
  }
}

Solution

MultiProvider/Provider had to be provided on top of the Widget tree. i.e., inside the runApp widget, which got rid of the ProviderNotFoundException., as now provider services are available throughout the app.

  Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  EcommerceApp.auth = FirebaseAuth.instance;
      
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (context) => Counter()),
      ChangeNotifierProvider(create: (context) => CartItemCounter()),
      ChangeNotifierProvider(create: (context) => AddressChangeCounter()),
      ChangeNotifierProvider(create: (context) => ItemQuantityCounter()),
      ChangeNotifierProvider(create: (context) => AddressChangeCounter()),
      ChangeNotifierProvider(create: (context) => TotalAmountCounter()),
    ],
    child: MyApp(),
  ));
}

Answered By – Nikash Deka

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *