代码之家  ›  专栏  ›  技术社区  ›  Saad Ebad

在视图中获取空值

  •  0
  • Saad Ebad  · 技术社区  · 3 年前

    我想获取库存数据,但不想获取。我在做没有模型的API集成,因为模型中存在一些问题,只是为了获取数据,并希望显示在我的视图中。

    这是我通过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;
      }
    

    这是我的控制器类,我正在使用上面的服务函数

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

    但当我使用inventoryData(在控制器中)访问数据时,我会得到null,但在控制器中,调试时我会得到值。但我不明白为什么我在视图中接收空值。

    这是我的观点,

     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}"),
        
        
        
                  );
    
    1 回复  |  直到 3 年前
        1
  •  0
  •   Rodrigo Molero    3 年前

    如果你的回答。statusCode不是200,这可能是因为您设置了错误的标题:

    'Authorization': 'Barear $accessToken'
    

    将其更改为:

    'Authorization': 'Bearer $accessToken'