基本上我使用一個 API,我可以在其中獲取車牌并顯示有關車輛的資訊。我設法讓一切正常,但似乎無法弄清楚如何顯示所有資訊。
Future<Album> fetchAlbum() async {
final response = await http
.get(Uri.parse('https://v1.motorapi.dk/vehicles/CZ33849'),
headers: {"X-AUTH-TOKEN": "rfrzsucnc7eo3m5hcmq6ljdzda1lz793",
"Content-Type": "application/json",
"Accept": "application/json",
});
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Album.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
//headers: {"X-AUTH-TOKEN": "rfrzsucnc7eo3m5hcmq6ljdzda1lz793"}
class Album {
final String? registration_number;
final String? status;
final String? type;
final String? use;
final String? first_registration;
final String? vin;
final int? doors;
final String? make;
final String? model;
final String? variant;
final String? model_type;
final String? color;
final String? chasis_type;
final int? engine_power;
final String? fuel_type;
final String? RegistreringssynToldsyn;
final String? date;
final String? result;
Album({
this.registration_number,
this.status,
this.type,
this.use,
this.first_registration,
this.vin,
this.doors,
this.make,
this.model,
this.variant,
this.model_type,
this.color,
this.chasis_type,
this.engine_power,
this.fuel_type,
this.RegistreringssynToldsyn,
this.date,
this.result,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
registration_number: json['registration_number'],
status: json['status'],
type: json['type'],
use: json['use'],
first_registration: json['first_registration'],
vin: json['vin'],
doors: json['doors'],
make: json['make'],
model: json['model'],
variant: json['variant'],
model_type: json['model_type'],
color: json['color'],
chasis_type: json['chasis_type'],
engine_power: json['engine_power'],
fuel_type: json['fuel_type'],
RegistreringssynToldsyn: json['RegistreringssynToldsyn'],
date: json['date'],
result: json['result'],
);
}
}
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late Future<Album> futureAlbum;
@override
void initState() {
super.initState();
futureAlbum = fetchAlbum();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Fetch Data Example'),
),
body: Center(
child: FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text("${snapshot.data!.use}");
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
),
),
),
);
}
}
當我編輯這一行時:return Text("${snapshot.data!.use}");,我可以用 Album 類中的其他變數替換 use ,但我似乎無法弄清楚如何一次顯示它。
uj5u.com熱心網友回復:
你想怎么顯示?這取決于您的用戶界面。如果你想把它全部放在一個文本小部件中,你可以這樣做
class User {
String name;
String address;
toString() {
return "name: " name ", address: " address;
}
}
或者您可以使用列/行小部件來顯示每個文本小部件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/380696.html
上一篇:如何從R中的資料集創建贏/輸記錄
下一篇:Flutter:列文本需要居中
