'Getting null values in View Flutter

I want to get data of inventory but not getting. I am doing API integration without model because there are some issues in Model just to get data and want to display in to my view.

this is my service class of get data through API.

Future<dynamic> getInventory() async {
    var data;
    String? userId = await preferenceService.getuserId();
    String? accessToken = await preferenceService.getAccessToken();

    var response = await http.get(Uri.parse('${AppUrl.getInventory}/$userId'),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
          'Authorization': 'Barear $accessToken'
        });

    print("The data of the specific inventory ===========>>>>>>>> " +
        response.body.toString());
    if (response.statusCode == 200) {
      data = jsonDecode(response.body);
      print('This is futr dsta --->>>   $data');
    } else {
      data=[];
    }
    return data;
  }

This is my controller class where i am using above service function

Future getMyInvenoryFromService() async {
    try {
      
      isLoadingInventory(true);
      await inventoryService.getInventory().then((val) {
        if (val != []) {
          inventoryData = val;
         
        } else {
          inventoryData = [];
        }
      });
      
    } finally {
      isLoadingInventory(false);
    }
  }

But when i am accessing the data with inventoryData (in controller) i am getting null, but in controller i am getting values when debugging. but i am not understanding why i am receiving null values in view.

This is my view,

 class _UserInventoryScreenState extends State<UserInventoryScreen> {
      InventoryController inventoryController = Get.put(InventoryController());
      InventoryService inventoryService = InventoryService();
      GiftController giftController = Get.put(GiftController());
      GiftStorageService giftStorageService = GiftStorageService();
    
      @override
      void initState() {
        super.initState();
        /*Future delay() async {
          await new Future.delayed(new Duration(milliseconds: 3000), () {
            inventoryController.getMyInvenoryFromService();
          });
        }*/
    
        Timer.run(() {
          inventoryController.getMyInvenoryFromService();
         
        });
      }
    
      @override
      Widget build(BuildContext context) {
       
        return Scaffold(
            backgroundColor: AppColors.pinkAppBar,
            appBar: AppBar(
              elevation: 0,
              backgroundColor: Colors.transparent,
              leading: InkWell(
                  onTap: () {
                    Get.back();
                  },
                  child: Icon(Icons.arrow_back)),
              title: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Inventory'),
                  InkWell(
                    onTap: () {
                      Get.to(AddInventoryScreen());
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                      decoration:
                          BoxDecoration(border: Border.all(color: Colors.white)),
                      child: Text(
                        "Add Inventory",
                        style: TextStyle(fontSize: 16),
                      ),
                    ),
                  )
                ],
              ),
            ),
            body: Obx(() {
              return inventoryController.isLoadingInventory.value == true
                  ? Center(child: CircularProgressIndicator())
                  : ElevatedButton(
                      onPressed: () async {
                        await inventoryController.getMyInvenoryFromService();
                      
                      },
                      child: Text("${inventoryController.inventoryData.length}"),
    
    
    
              );


Solution 1:[1]

If your response.statusCode isn't 200 it might be because you are setting wrong your headers:

'Authorization': 'Barear $accessToken'

Change it to:

'Authorization': 'Bearer $accessToken'

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Rodrigo Molero