0

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();
      },
    );
  }
}
Malak
  • 336
  • 2
  • 13

3 Answers3

2

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();
              },
            ),
          ],
        );
      },
    );
  }
}
fany fernaldi
  • 279
  • 2
  • 4
1

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()),
Eimantas G
  • 437
  • 3
  • 7
  • check https://stackoverflow.com/questions/57335980/changenotifierprovider-vs-changenotifierprovider-value for `ChangeNotifierProvider vs ChangeNotifierProvider.value` – nagendra nag Jan 29 '22 at 10:39
0

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

reza
  • 1,354
  • 1
  • 7
  • 25