Data not being passed to second stateful class

Greetings, I wrote some logic to retrieve a json object and pass it through a series of widgets for presenting on my iOS/Android application. My issue is that the data doesn’t reach the next widget that is set to receive it. I’ve been trying all day to get this functionality up and running – can’t seem to spot why its not getting the values. My code will be below

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,
    );
  }
}

How are you constructing a PortalNavigator?

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

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

my setup for the class is above.

That shouldn’t have even compiled. You went from named parameters to positional parameters.

interesting. I’m surprised that I didn’t receive any failures at all.

I highly recommend all new projects start with package:very_good_analysis to ensure you’re coding to the highest possible standards.

1 Like

You mean it doesn’t update the data inside the portal navigator when the parent is rebuild with new data?

Did you overwrite “didUpdateWidget” in the portalnavigator as that is state full too?

@RandalSchwartz where did he change from named to positional or vice versa? I only see named parameters

isn’t initialized anywhere in _PortalNavigatorState.

I always initialize such parameters in initState or didUpdateWidget like here ( taken from Andrea Bizzotto’s GitHub - bizz84/complete-flutter-course: Flutter Foundations Course - eCommerce App )

class LeaveReviewForm extends ConsumerStatefulWidget {
  const LeaveReviewForm({super.key, this.review});
  final Review? review;

  @override
  ConsumerState<LeaveReviewForm> createState() => _LeaveReviewFormState();
}

class _LeaveReviewFormState extends ConsumerState<LeaveReviewForm> {

  @override
  void initState() {
    super.initState();
    final review = widget.review;
    if (review != null) {
      _controller.text = review.comment;
    }
  }
1 Like

How is it supposed to pass the user info from the StatefulWidget (PortalNavigator) to the State (_PortalNavigatorState)? As Randal said, I’m truly surprised this compiled at all.

If I try and do what you are doing I get the errors I’d expect. Looks like you must not have any lints setup:

Or they’re showing up in the error window and the error window is being ignored.

But can it compile with these errors?

It shouldn’t be able to.

1 Like