'Flutter Sending variable with api data to another class

I have an app which gets data from the backend. I used a FutureBuilder and for the most classes when i try to pass the variable that im getting data, it's successfully being send to the other classes and i use the variable. But in another case i try to pass the variable to the other class and it's failing saying that null is being returned from the api.

return Scaffold(
  body: FutureBuilder<Response>(
    future: futureDataForStatus,
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        WorkingLocationStatus accountInfo = WorkingLocationStatus.fromJson(
          json.decode(snapshot.data!.body),
        );
 
        sendAccountInfo(){
         return IsUserWorking(accountStatus: accountInfo.status,);
        }
-------------
 
class IsUserWorking extends StatelessWidget {
  const IsUserWorking({
    Key? key,
    this.accountStatus,
  }) : super(key: key);
 
  final String? accountStatus; // according to debugger its returning null 
 
  @override
  Widget build(BuildContext context) {
    if (accountStatus == 'NOT_WORKING') {
      return const WorkingScreen();
    }
    return const StopWorkingScreen();
  }
}
 
------ 
 
// what the api returns 
 
when the user is not working api returns this
 
{"status":"NOT_WORKING"}
 
but if the user is working it returns this
 
{"status":"WORKING","name":{"locationName":"Gjakova e Re","location":"Rruga Besmir Haxhi Koci, nr 577","startTime":"2022-02-28T21:16:38.510879+01:00","endTime":null,"duration":"PT10.256862755S"}}

// same variable passing to other classes

return Scaffold(
  body: FutureBuilder<Response>(
    future: futureDataForStatus,
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        WorkingLocationStatus accountInfo = WorkingLocationStatus.fromJson(
          json.decode(snapshot.data!.body),
        );
 
LocationNameData(accountInfo: accountInfo), //works fine
LocationData(accountInfo: accountInfo), // works fine
WorkingStartTime(accountInfo: accountInfo), // works fine
 
class LocationNameData extends StatelessWidget {
  const LocationNameData({
    Key? key,
    required this.accountInfo,
  }) : super(key: key);
 
  final WorkingLocationStatus accountInfo;
 
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Align(
          alignment: Alignment.centerLeft,
          child: Padding(
            padding: const EdgeInsets.only(left: 30),
            child: RichText(
              text: TextSpan(
                style: DefaultTextStyle.of(context).style,
                children: [
                  const TextSpan(
                    text: 'Status: ',
                    style: TextStyle(
                      fontSize: 18,
                      color: Color(0xFF616161),
                    ),
                  ),
                  TextSpan(
                    text: accountInfo.status, // works just fine
                    style: const TextStyle(
                        fontSize: 18,
                        color: Colors.green,
                        fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    );

// function to fetch the data

Future<Response> getLocationStatus(BuildContext context) async {
  final navigator = GlobalKey<NavigatorState>();

  SharedPreferences prefs = await SharedPreferences.getInstance();
  String? authorization = prefs.getString('authorization');
  var url = 'url';
  locationStatus = await http.get(
    Uri.parse(url),
    headers: <String, String>{
      'authorization': authorization ?? basicAuth.toString(),
      "Content-Type": "application/json"
    },
  );
  print('qqqqqqqqqqqq${locationStatus!.statusCode}');
  return locationStatus!;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source