我知道你很好,请你帮助我,
我尝试从 flutter 的第一页获取数据,
我很好地获取了数据,但是当我想从这些数据中获取名称时,我总是得到 null,
发生了什么事
这是我的第一页(这里一切都很好)
import 'dart:convert';
import 'package:api_app/model/cinema.dart';
import 'package:api_app/model/villes.dart';
import 'package:api_app/pages/movies.dart';
import 'package:api_app/services/http_service_local.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import '../configGlobals/globalparams.dart';
class Mycinema extends StatefulWidget {
const Mycinema({super.key});
@override
State<Mycinema> createState() => _MycinemaState();
}
class _MycinemaState extends State<Mycinema> {
HttpServiceLocal apiService = HttpServiceLocal(); // Instancier le service API
//objet Map
Map<String, dynamic> villeData = {};
@override
void initState() {
super.initState();
loadVille();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Cinema", style: TextStyle(color: Colors.white),),backgroundColor: Colors.purpleAccent,),
body:Center(
child: villeData.isEmpty
? const CircularProgressIndicator()
: ListView.builder(
itemCount: (villeData.length ?? 0),
itemBuilder: (context, index){
String key = villeData.keys.elementAt(index);
var v = villeData[key]; // Accéder aux données de la ville via la clé
return Card(
color: Colors.purpleAccent,
child:Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton.icon(
onPressed:(){
Navigator.push(context, MaterialPageRoute(builder: (ctx)=>Movies(villeData)));
},
label: Text(v['name'] ?? 'Pas de titre')),
),
);
}),
)
);
}
void loadVille() async {
QuerySnapshot querySnapshot = await apiService.fetchVille(); // Appel API GET
// Boucler à travers les documents et les stocker dans un objet Map
Map<String, dynamic> tempData = {};
for (var doc in querySnapshot.docs) {
tempData[doc.id] = doc.data(); // Stocke les données dans un Map
}
// Mise à jour de l'état avec les données récupérées
setState(() {
villeData = tempData;
print("donnée de la ville ${villeData}");
});
}
}
在我的第二页 我总是获取数据,但没有这些数据的名称
import 'package:flutter/material.dart';
class Movies extends StatefulWidget {
dynamic villeData;
Movies(this.villeData);
@override
State<Movies> createState() => _MoviesState();
}
class _MoviesState extends State<Movies> {
@override
Widget build(BuildContext context) {
print("reception ${widget.villeData} ");
print('name ${widget.villeData['name'] } ');
return Scaffold(
appBar: AppBar(title: Text("Cinema de ${widget.villeData["name"]} ", style: const TextStyle(color: Colors.white),),backgroundColor: Colors.purpleAccent,),
body: Center(
child: Text("List cinema"),
),
);
}
}
我的控制台
I/flutter (13932): reception {0000344455022492: {cinemas: null, altitude: 45.78, latitude: 43.67, name: Cameroun, id: 0000344455022492, longitude: 45.4}, 00003444550226712: {altitude: 45.78, cinemas: null, latitude: 43.67, name: Maroc, id: 00003444550226712, longitude: 45.4}, 122334: {cinemas: null, altitude: 45.78, latitude: 43.67, name: Paris, id: 122334, longitude: 45.4}, 122335: {altitude: 45.78, cinemas: null, latitude: 43.67, name: RDCongo, id: 122335, longitude: 45.4}, vl00003444550223119: {altitude: 45.78, cinemas: null, latitude: 43.67, name: Kinshasa, id: vl00003444550223119, longitude: 45.4}}
**I/flutter (13932): name null**
您似乎误解了数据的结构。 您要查找的名称位于
receptionId
下方
应该是这样的
widget.villeData['0000344455022492']['name']