有状态 Widget 不接收从正在进行的 Widget | 传递给其构造函数的数据飞镖扑动

问题描述 投票:0回答:1

我花了大约 5 个小时调试两个小部件之间的连接,这两个小部件在接收数据的直通点停止运行。我对 Flutter 相当陌生,这将是我有史以来的第一个项目。我查看了 stackoverflow,没有一个答案适用于最新版本的 flutter。如何让数据在两个或更多有状态小部件之间传递?

// Works
class ClientPortal extends StatefulWidget {
  const ClientPortal({
    super.key,
  });

  @override
  State<ClientPortal> createState() => _ClientPortalState();
}

class _ClientPortalState extends State<ClientPortal> {
  // State Variables
  dynamic userData;
  dynamic dataCount;

  fetchClientPortalData() async {
    final storage = FlutterSecureStorage();
    final storageObj = await storage.readAll();
    var uid = storageObj['uid'];
    var accessToken = storageObj['access_token'];
    var client = storageObj['client'];

    try {
      Response response = await post(
          Uri.parse('http://localhost:3000/api/v1/clients/client_portal'),
          body: {'uid': uid, 'access-token': accessToken, 'client': client});
      print(response.statusCode);
      if (response.statusCode == 200) {
        var clientPortalData = jsonDecode(response.body.toString());
        var responseHeaders = response.headers;
        setState(() {
          dataCount = clientPortalData['dataCount'];
          userData = clientPortalData['userData'];
        });
      }
    } catch (e) {
      print(e);
    }
  }

  late Future<dynamic> _clientPortalData;
  @override
  void initState() {
    _clientPortalData = fetchClientPortalData();
    super.initState();
  }

  Widget build(BuildContext context) {
    return FutureBuilder<dynamic>(
      future: _clientPortalData,
      initialData: {},
      builder: (context, snapshot) {
        List<Widget> children;
        if (snapshot.connectionState == ConnectionState.done) {
          print('INSIDE OF CLIENT PORTAL');
         // works start
            print(userData);
        // works end

           // fails to store data attributes here in widget below
          return PortalNavigator(
              dataCount: dataCount,
              userData: userData);
        } else {
        }
        return Center(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: children,
        ));
      },
    );
  }
}


// THIS WIDGET DOES NOT RECEIVE DATA
class PortalNavigator extends StatefulWidget {
  PortalNavigator(
      {super.key,
              dataCount: dataCount,
              userData: userData});

  @override
  State<PortalNavigator> createState() => _PortalNavigatorState();
}


class _PortalNavigatorState extends State<PortalNavigator> {
  dynamic userData;
  dynamic dataCount;

  @override
  Widget build(BuildContext context) {
    print('NO DATA EXIST FOR VARS ABOVE');
   print(userData);
   print(dataCount);

    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: PortalNavigatorView(
              dataCount: dataCount,
              userData: userData),
      debugShowCheckedModeBanner: false,
    );
  }
}

class PortalNavigator extends StatefulWidget {
  PortalNavigator(
      {super.key,
      dataCount, userData});

  @override
  State<PortalNavigator> createState() => _PortalNavigatorState();
}

I tried to initialize the variables for the faulty widget but received errors further down the line.
javascript flutter dart
1个回答
0
投票

当你想从父控件获取数据时,在构造函数中定义参数就足够了。因此,首先您可以从这个

userData
类中删除
dataCount
class _PortalNavigatorState extends State<PortalNavigator>
。 每当您想使用接收到的数据时,请像
widget.userData
widget.dataCount
那样称呼它。
widget
关键字对于在每个有状态小部件中调用构造函数参数至关重要。

class PortalNavigator extends StatefulWidget {
  PortalNavigator(
      {super.key,
              required this.dataCount,
              required this.userData});
    final dynamic dataCount;
    final dynamic userData;

  @override
  State<PortalNavigator> createState() => _PortalNavigatorState();
}


class _PortalNavigatorState extends State<PortalNavigator> {

  @override
  Widget build(BuildContext context) {
    print('NO DATA EXIST FOR VARS ABOVE');
   print(widget.userData);
   print(widget.dataCount);

    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: PortalNavigatorView(
              dataCount: widget.dataCount,
              userData: widget.userData),
      debugShowCheckedModeBanner: false,
    );
  }
}

class PortalNavigator extends StatefulWidget {
  PortalNavigator(
      {super.key,
      required this.userData,
required this.dataCount});
final dynamic userData;
final dynamic dataCount;

  @override
  State<PortalNavigator> createState() => _PortalNavigatorState();
}
© www.soinside.com 2019 - 2024. All rights reserved.