This is likely a mistake, as Provider will not automatically update dependents

Issue

whenever I add the provider to the MultipleProvider it just shows this weird error not not able to solve it after spending 4 hours.

main.dart

MultiProvider(
      providers: [
        Provider<HandleImageSelectionModel>(
            create: (_) => HandleImageSelectionModel()),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        initialRoute: '/',
        onGenerateRoute: RouteGenerator.generateRoute,
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
      ),
    );

Provider class

import 'package:flutter/foundation.dart';

class HandleImageSelectionModel extends ChangeNotifier {
  bool isSelectionModeEnabled = false;
  HandleImageSelectionModel();
  toggleSelectionMode() {
    isSelectionModeEnabled = !isSelectionModeEnabled;
    notifyListeners();
  }
}

Changing State

Provider.of<HandleImageSelectionModel>(context)
                  .toggleSelectionMode();

Trying to consume here

Consumer<HandleImageSelectionModel>(
                    builder: (context, isEnabled, child) {
                      print(isEnabled);

                      return Positioned(
                        child: Align(
                          alignment: Alignment.topRight,
                          child: CircularCheckBox(
                              value: true,
                              materialTapTargetSize:
                                  MaterialTapTargetSize.padded,
                              onChanged: (bool x) {}),
                        ),
                      );
                    },
                  )

Solution

You’re using Provider when the class you pass is a ChangeNotifier.

Use ChangeNotifierProvider instead:

ChangeNotifierProxyProvider<HandleImageSelectionModel>

Answered By – Rémi Rousselet

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

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