簡體   English   中英

如何從 API 中獲取數據並存儲到 Flutter 中的變量中?

[英]How to fetch the data from the API and store into a variable in flutter?

我需要從 API 中獲取一些數據,並將數據存儲在變量中。

在 pubspec.yaml 中添加這個

dependencies:
      dio: ^4.0.6

//dart文件中的代碼

import 'package:dio/dio.dart';
void getHttp() async {
  try {
    var response = await Dio().get('http://www.google.com');
    print(response);
  } catch (e) {
    print(e);
  }
}

您應該考慮創建一個要放置 json 數據的對象。 您更有可能使用 http 包從 api 檢索數據,然后將其存儲在您創建的對象中。 通常將 api 數據格式化為 json 格式,在這種情況下,您使用 dart:convert 來序列化 JSON。 為了顯示輸出,您將使用 getter 從對象中檢索數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM