我花了大約一個小時在網上某處尋找這個解決方案。我是 Flutter 和 Dart 語言的新手,但我對 C# 和 .net 非常熟悉。即使 dart/flutter 使用 C# 語法,很多語言的感覺也與我想象的大不相同。
我在 .net 中有一個寧靜的 API,它回傳一個字串的 json 物件:字串和字串:[字串陣列]。我在顫振中有一個物件類,我可以在其中反序列化回應。我已經用 List 和 String 的正常回應做到了這一點,沒有問題,但現在我遇到了大問題。我不知道如何反序列化看起來像這樣的 Json。
按照要求
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
centerTitle: true,
backgroundColor: Colors.blueGrey,
foregroundColor: Colors.white,
),
drawer: const NavigationDrawer(),
body: Column(
children: [
Center(
child: Text(templateName),
),
Center(
child: FutureBuilder<TemplateContentAndArgumentsObject>(
future: templateContent,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(snapshot.data?.TemplateContent ?? "null");
} else {
return CircularProgressIndicator();
}
},
),
),
],
),
);
API呼叫代碼
Future<TemplateContentAndArgumentsObject> getTemplateContent(
String customerId, String templateName) async {
var url = Uri.parse(
'https://localhost:7167/api/v1/Templates/$customerId/$templateName');
var response = await http.get(url, headers: {
"Accept": "application/json",
"Access-Control-Allow-Origin": "*"
});
try {
print(response.body);
var sm = json.decode(response.body);
print(sm);
} catch (ex) {
print(ex);
}
if (response.statusCode == 200) {
TemplateContentAndArgumentsObject obj =
TemplateContentAndArgumentsObject.fromJson(json.decode(response.body));
print(obj.TemplateContent);
print(obj.TemplateArguments);
return obj;
} else {
print('Request failed with status: ${response.statusCode}');
}
return TemplateContentAndArgumentsObject(
TemplateContent: "", TemplateArguments: new List<String>.empty());
}
類物件
import 'package:flutter/cupertino.dart';
class TemplateContentAndArgumentsObject {
String TemplateContent;
List<String> TemplateArguments;
TemplateContentAndArgumentsObject({
required this.TemplateContent,
required this.TemplateArguments,
});
factory TemplateContentAndArgumentsObject.fromJson(
Map<String, dynamic> json,
) =>
TemplateContentAndArgumentsObject(
TemplateContent: json["TemplateContent"] as String,
TemplateArguments: (json["TemplateArguments"] as List<String>),
);
}
杰森的形象
uj5u.com熱心網友回復:
以下是您的問題的示例代碼。請注意,代碼只是根據您的示例創建的,其中您的串列僅包含String
物件。如果您的串列包含更高級的物件,我們應該將它們單獨建模并放入串列中。但是對于字串,您可以執行以下操作:
class TemplateContentAndArgumentsObject {
String myStringContent;
List<String> myArrayContent;
TemplateContentAndArgumentsObject({
required this.myStringContent,
required this.myArrayContent,
});
factory TemplateContentAndArgumentsObject.fromJson(
Map<String, dynamic> json,
) =>
TemplateContentAndArgumentsObject(
myStringContent: json["myStringContent"] as String,
myArrayContent:
(json["myArrayContent"] as List<dynamic>).cast<String>(),
);
Map<String, Object> toJson() => {
"stringContnet": myStringContent,
"arrayCOntnet": myArrayContent,
};
}
我已將建構式更改為fromJson
僅呼叫類建構式的工廠建構式。通過這樣做,它消除了宣告類變數的需要late
。
uj5u.com熱心網友回復:
嘿,你可以修改你的構建方法,你需要檢查條件snapshot.hasData
,更多細節見FutureBuilder
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
centerTitle: true,
backgroundColor: Colors.blueGrey,
foregroundColor: Colors.white,
),
drawer: const NavigationDrawer(),
body: Column(
children: [
Center(
child: Text(templateName),
),
Center(
child: FutureBuilder<TemplateContentAndArgumentsObject>(
future: templateContent,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data?.TemplateContent ?? "");
}else if (snapshot.hasError){
/// return error widget
return Container();
} else {
return CircularProgressIndicator();
}
},
),
),
],
),
);
uj5u.com熱心網友回復:
//映射到字串
Map<String, dynamic> mapData = { "username":name, "email":email, "phoneNumber":mobileNo, "password":password , "refCode":inviteCode, "countryCode":countryCode, "country":"印度” };
json.encode(mapData);
// 映射到串列
串列值 = List();
mapData.forEach((v) => values.add(v)); 列印(值);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/494760.html