NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: [](0))
NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: [](0))
我正在尝试创建一个应用程序,这个应用程序从 open-meteo API 获取信息。我不断遇到一些不知道如何修复的错误。非常感谢任何帮助!!
title: Text(snapshot.data[i].longitude), //这是产生错误的行
import 'dart:convert';
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
));
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future getUserData() async {
var response = await http.get(Uri.parse( 'https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m'));
var jsonData = jsonDecode(response.body);
List<User> users = [];
for (var u in (jsonData)) {
User user = User(u['longitude'], u['latitude'], u['hourly'], u['time'],u['temperature']);
users.add(user);
}
print(users.length);
return users;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Rangers Tool'),
),
body: Container(
child: Card(
child: FutureBuilder(
future: getUserData(),
builder: (context, AsyncSnapshot snapshot) {
return ListView.builder(
itemCount: snapshot.data?.length,
itemBuilder: (context, i) {
return ListTile(
title: Text(snapshot.data[i].longitude),
subtitle: Text(snapshot.data[i].latitude),
);
});
}
},
))), ); } }
class User {
final String longitude, latitude, hourly, time, temperature;
User(this.longitude, this.latitude, this.hourly, this.time, this.temperature); }
}
您需要添加一个条件,通过检查 snapshot
的状态来确保数据存在,然后再使用它来构建您的列表。类似这样:
FutureBuilder(
future: _fetchListItems(),
builder:(context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
SnapshotError(snapshot.error),
} else {
return ListView.builder(
itemCount: snapshot.data?.length,
itemBuilder: (context, i) {
return ListTile(
title: Text(snapshot.data[i].longitude),
subtitle: Text(snapshot.data[i].latitude),
);
}
}
)
我正在尝试创建一个应用程序,这个应用程序从 open-meteo API 获取信息。我不断遇到一些不知道如何修复的错误。非常感谢任何帮助!!
title: Text(snapshot.data[i].longitude), //这是产生错误的行
import 'dart:convert';
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
));
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future getUserData() async {
var response = await http.get(Uri.parse( 'https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m'));
var jsonData = jsonDecode(response.body);
List<User> users = [];
for (var u in (jsonData)) {
User user = User(u['longitude'], u['latitude'], u['hourly'], u['time'],u['temperature']);
users.add(user);
}
print(users.length);
return users;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Rangers Tool'),
),
body: Container(
child: Card(
child: FutureBuilder(
future: getUserData(),
builder: (context, AsyncSnapshot snapshot) {
return ListView.builder(
itemCount: snapshot.data?.length,
itemBuilder: (context, i) {
return ListTile(
title: Text(snapshot.data[i].longitude),
subtitle: Text(snapshot.data[i].latitude),
);
});
}
},
))), ); } }
class User {
final String longitude, latitude, hourly, time, temperature;
User(this.longitude, this.latitude, this.hourly, this.time, this.temperature); }
}
您需要添加一个条件,通过检查 snapshot
的状态来确保数据存在,然后再使用它来构建您的列表。类似这样:
FutureBuilder(
future: _fetchListItems(),
builder:(context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
SnapshotError(snapshot.error),
} else {
return ListView.builder(
itemCount: snapshot.data?.length,
itemBuilder: (context, i) {
return ListTile(
title: Text(snapshot.data[i].longitude),
subtitle: Text(snapshot.data[i].latitude),
);
}
}
)