我的提供程序有问题:抛出 ProviderNotFoundException(T, context.widget.runtimeType);

I have problem with provider : throw ProviderNotFoundException(T, context.widget.runtimeType);

提问人:Badriya Haddad 提问时间:10/28/2022 最后编辑:Md. Yeasin SheikhBadriya Haddad 更新时间:10/28/2022 访问量:187

问:

我刚刚开始进行状态管理,我的第一次尝试是使用 Provider,我在 youtube 上观看了一个教程,但无法处理我的代码,所以我复制和粘贴,也无法正常工作。 所以这个想法是,有一个主屏幕 此主屏幕包含ListTile中的人员列表,此ListTile具有尾随图标,您知道在显示收藏夹人员列表的收藏夹列表屏幕中添加和删除人员 我不知道为什么,但这是我的代码: 这是 main.dart,其根目录是:

import 'package:flutter/material.dart';
import 'package:note/Designs/Design_one.dart';
import 'package:note/Designs/Favorite_List.dart';
import 'package:note/Designs/another.dart';
import 'package:note/Screens/Random_Words.dart';
import 'package:provider/provider.dart';
import 'Models/Favorite_List_Provider.dart';
import 'Screens/Hello_World.dart';
import 'Screens/Stateful_Widget.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) {
            favorite();
          },
        ),
      ],
     child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Demo',
        theme: ThemeData(
          // Add the 5 lines from here...
          appBarTheme: const AppBarTheme(
              backgroundColor: Colors.white,
              foregroundColor: Colors.black,
              shadowColor: Colors.blue,
              surfaceTintColor: Colors.orange,
              shape:
                  Border(bottom: BorderSide(color: Colors.orange, width: 4))),
        ),
        home: HomeScreen(),
        // DesignOne(
        //   Desc: '',
        //   Image: '',
        // ),
      ),
    );
  }
}

这是我扩展 changeprovider 时的提供程序:

import 'package:flutter/material.dart';

class favorite extends ChangeNotifier {
  final List<String> _favoriteList = [];

  List<String> get favoriteList => _favoriteList;
 

  add(String name) {
    _favoriteList.add(name);
    notifyListeners();
  }

  remove(String name) {
    _favoriteList.removeWhere((element) => element == name);
    notifyListeners();
  }
}

所以这段代码应该是主屏幕:

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:note/Designs/Favorite_List.dart';
import 'package:note/Models/Favorite_List_Provider.dart';
import 'package:note/Models/Food_Models.dart';
import 'package:provider/provider.dart';

import '../Services/Fetch_Data.dart';
import 'Details_DesignOfDesignOne.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  ListTile eachTile(favorite favorite, String name, String initials) {
    return ListTile(
      title: Text(name),
      leading: CircleAvatar(
        child: Text(initials),
      ),
      trailing: IconButton(
        icon: (favorite.favoriteList.contains(name))
            ? Icon(Icons.check)
            : Icon(Icons.add),
        onPressed: () {
          if (favorite.favoriteList.contains(name)) {
            favorite.remove(name);
          } else {
            favorite.add(name);
          }
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Provider"),
        centerTitle: true,
        actions: <Widget>[
          ElevatedButton(
              child: Text(
                "next",
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => FavoriteScreen(),
                  ),
                );
              }),
        ],
      ),
      body: Consumer<favorite>(
        builder: (context, favorite value, child) {
          return ListView(
            children: <Widget>[
              eachTile(value, "Elon Musk", "EM"),
              eachTile(value, "Kanye West", "KW"),
              eachTile(value, "Kim Kardashian", "KK"),
              eachTile(value, "Kobe Bryant", "KB"),
              eachTile(value, "Tom Hanks", "TH"),
              eachTile(value, "Lebron James", "LJ"),
              eachTile(value, "Michael Jordan", "MJ"),
              eachTile(value, "Joe Rogan", "JR"),
              eachTile(value, "Selena Gomez", "SG"),
              eachTile(value, "Oprah Winfrey", "OW"),
            ],
          );
        },
      ),
    );
  }
}

这应该显示来自主屏幕的最喜欢的人列表:

import 'package:flutter/material.dart';
import 'package:note/Models/Favorite_List_Provider.dart';
import 'package:provider/provider.dart';

class FavoriteScreen extends StatefulWidget {
  @override
  State<FavoriteScreen> createState() => _FavoriteScreenState();
}

class _FavoriteScreenState extends State<FavoriteScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Profile"),
          centerTitle: true,
        ),
        body: Consumer<favorite>(
          builder: (context, favorite value, child) {
            return ListView.builder(
                itemCount: value.favoriteList.length + 1,
                itemBuilder: (context, index) {
                  if (index == 0) {
                    return Padding(
                      padding: EdgeInsets.all(40.0),
                      child: Text("Your Favorite List:"),
                    );
                  } else {
                    return ListView(children: [
                      ListTile(
                        title: Text(value.favoriteList[index - 1]),
                      ),
                    ]);
                  }
                });
          },
        ));
  }
}

这是例外:

enter image description here

flutter dart 提供者 flutter-provider

评论


答:

0赞 Md. Yeasin Sheikh 10/28/2022 #1

您需要从 开始返回实例create

providers: [
  ChangeNotifierProvider(
    create: (context) {
      return favorite(); //this one here
    },
  ),
],