简体   繁体   中英

MultiProvider executing function Flutter

i have Text in GestrureDetector widget, i want to execute my function showNameAction by clicking on text "imie".

Error:

" Could not find the correct Provider above this FriendsPageEditable Widget "

FriendsEditablePage.dart: (short version) (stateless class)

 return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: SaveBoxes()),
        ],
        child: Scaffold(
body: GestureDetector(
  onTap: () {
             Provider.of<SaveBoxes>(context, listen: false).showNameAction;
            },
   child: Text(imie))),

SaveBoxes.dart

class SaveBoxes extends ChangeNotifier {

  void showNameAction(BuildContext context) {
    showDialog<void>(
      context: context,
       builder: (context) {
         return AlertDialog();
      },
    );
  }
}

This line of code looks not right to me:

ChangeNotifierProvider.value(value: SaveBoxes()),

You are using ChangeNotifierProvider.value widget which is used when you already have an instance and don't want create a new one. But in value: SaveBoxes() you create an instance. So try changing it to:

ChangeNotifierProvider(create: (context) => SaveBoxes()),

You can use Multiprovider in main.dart and call SaveBoxes saveBoxes = Provider.of<SaveBoxes>(context) in friendseditablepage.dart

This is the source code based on your case. Hope it work:

main.dart file:

void main() async {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => SaveBoxes(),
        ),
      ],
      child: const MaterialApp(
        home: FriendsEditablePage(),
      ),
    );
  }
}

friendseditablepage.dart file:

class FriendsEditablePage extends StatelessWidget {
  const FriendsEditablePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SaveBoxes saveBoxes = Provider.of<SaveBoxes>(context);

    return Scaffold(
      body: SafeArea(
        child: GestureDetector(
          onTap: () {
            saveBoxes.showNameAction(context);
          },
          child: const Text('imie'),
        ),
      ),
    );
  }
}

saveboxes.dart file:

class SaveBoxes extends ChangeNotifier {
  void showNameAction(BuildContext context) {
    showDialog<void>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('AlertDialog Title'),
          content: SingleChildScrollView(
            child: ListBody(
              children: const <Widget>[
                Text('This is a demo alert dialog.'),
                Text('Would you like to approve of this message?'),
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text('Approve'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

Try to wrap your GestureDetector inside a Builder so you can access the updated context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM