无法将ChangeNotifierProvider.value放在扑波中以反映数据中的变化

发布于 2025-01-17 19:27:39 字数 3145 浏览 2 评论 0原文

我无法在 UI 中放置changeNotifierProvider.value 来显示更改(已检查并且数据没有问题),如果您可以,请指导我在下面的小部件中放置changeNotifierProvider.value 的位置和方式。谢谢。

如果您需要任何进一步的信息,请告诉我。

更改.dart

 @override
  Widget build(BuildContext context) {
    final check = Provider.of<UserProfileReadProvider>(context);

    final n = check.items;

    return Scaffold(
      body: Container(
        margin:
            const EdgeInsets.only(top: 50, left: 40, right: 10, bottom: 90.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            CircleAvatar(
              radius: 50.0,
              backgroundColor: Colors.blue.shade800,
              child: const Text('AM'),
            ),
            const SizedBox(height: 70),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'Name : ${n.name}',
                  style: const TextStyle(
                      fontStyle: FontStyle.normal,
                      color: Colors.black,
                      fontSize: (18.0)),
                ),
                const SizedBox(height: 15),
                Text(
                  'Address: ${n.address}',
                  style: const TextStyle(
                      fontStyle: FontStyle.normal,
                      color: Colors.black,
                      fontSize: 18.0),
                ),

main.dart

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (ctx) => UserProfileReadProvider(),
          ),
        ],
        child: MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          initialRoute: '/',
          routes: {
            '/': (ctx) =>Page(),
          },
        ));
  }
}

提供程序.dart


UserProfileInformation nitems = UserProfileInformation();

Future<void> addUserinformation(
      UserProfile st, var imagecheck) async {
    
    Map<String, String> headers = {
      "Content-Type": "charset=utf-8",
      "Content-type": "application/json"
    };

    var uri = Uri.parse('http://localhost:3001/userprofile/check/upload');
    try {
      var request = http.MultipartRequest('POST', uri);
      request.headers.addAll(headers);
      request.fields['userId'] = st.userId;
      request.fields['name'] = st.name;
      request.fields['address'] = st.address;

      var response = await request.send();

      print(response.statusCode);
      if (response.statusCode == 201) {
        st = nitems;

        notifyListeners();
      } else {
        print('invalid data ');
      }
    } catch (error) {
      print(error);
    }
  }

I am unable to place changeNotifierProvider.value for the UI to show the changes( checked and no issue with the data), If you can please guide me as to where and how to place the changeNotifierProvider.value in the below widget.Thank you.

Please let me know if u require any further information.

change.dart

 @override
  Widget build(BuildContext context) {
    final check = Provider.of<UserProfileReadProvider>(context);

    final n = check.items;

    return Scaffold(
      body: Container(
        margin:
            const EdgeInsets.only(top: 50, left: 40, right: 10, bottom: 90.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            CircleAvatar(
              radius: 50.0,
              backgroundColor: Colors.blue.shade800,
              child: const Text('AM'),
            ),
            const SizedBox(height: 70),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'Name : ${n.name}',
                  style: const TextStyle(
                      fontStyle: FontStyle.normal,
                      color: Colors.black,
                      fontSize: (18.0)),
                ),
                const SizedBox(height: 15),
                Text(
                  'Address: ${n.address}',
                  style: const TextStyle(
                      fontStyle: FontStyle.normal,
                      color: Colors.black,
                      fontSize: 18.0),
                ),

main.dart

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (ctx) => UserProfileReadProvider(),
          ),
        ],
        child: MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          initialRoute: '/',
          routes: {
            '/': (ctx) =>Page(),
          },
        ));
  }
}

provider.dart


UserProfileInformation nitems = UserProfileInformation();

Future<void> addUserinformation(
      UserProfile st, var imagecheck) async {
    
    Map<String, String> headers = {
      "Content-Type": "charset=utf-8",
      "Content-type": "application/json"
    };

    var uri = Uri.parse('http://localhost:3001/userprofile/check/upload');
    try {
      var request = http.MultipartRequest('POST', uri);
      request.headers.addAll(headers);
      request.fields['userId'] = st.userId;
      request.fields['name'] = st.name;
      request.fields['address'] = st.address;

      var response = await request.send();

      print(response.statusCode);
      if (response.statusCode == 201) {
        st = nitems;

        notifyListeners();
      } else {
        print('invalid data ');
      }
    } catch (error) {
      print(error);
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文