我使用 Wix 創建了一個資料庫,其中包含多種不同型別的內容,例如字串、影像、地址等。我想將 WIX 資料庫中的資訊用于應用程式(使用Flutter和Dart 制作);只是在 ListView 中描繪資訊,但似乎資料沒有到達應用程式。
我在 Wix 上創建了必要的功能,以使第三方可以訪問該資料庫,并使用 Postman 對其進行了測驗。當我使用此 URL ( https://daudadmin.editorx.io/acteeventpage/_functions/regions )發出請求時,它作業正常,郵遞員將所有資訊作為 JSON 回傳。
現在,當我在應用程式代碼中使用 Url 時;它只是回傳一個空白頁。那是我目前使用的代碼:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final url = 'https://daudadmin.editorx.io/acteeventpage/_functions/regions';
var _postsJson = [];
void fetchData() async {
try {
final response = await get(Uri.parse(url));
final jsonData = jsonDecode(response.body) as List;
setState(() {
_postsJson = jsonData;
});
} catch (err) {
//handle error here with error message
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
fetchData();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ListView.builder(
itemCount: _postsJson.length,
itemBuilder: (context, i) {
final post = _postsJson[i];
return Text("Title: ${post["title"]}");
}),
),
);
}
}
uj5u.com熱心網友回復:
這是導致您錯誤的行
final jsonData = jsonDecode(response.body) as List;
從端點回傳的資料是 Map 而不是 List。
{
"items": [
{},
...
],
}
要訪問串列,請嘗試通過將宣告更改為來訪問地圖的 items 屬性
final jsonData = jsonDecode(response.body)["items"] as List;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/350632.html
標籤:镖 http请求 速度 颤振-http dart-http
