Flutter - NoSuchMethodError:方法"visitChildren"在 null 上被调用



我刚开始在youtube上学习。我试图构建一个加密货币应用程序,该应用程序将显示从中获取数据的加密货币的价格和名称https://api.coinmarketcap.com/v1/ticker/?limit=50.我写的代码没有任何编译错误。

VS CODE没有给我任何错误。但当我在网络和安卓系统上运行该应用程序时,它会抛出一个错误,上面写着:NoSuchMethodError:对null调用了方法"visitChildren"。接收方:空尝试调用:visitChildren(Clousre:(InlineSpan(=>bool(

我有两个文件1.main.dart


import 'package:flutter/material.dart';
import 'HomePage.dart';
import 'package:http/http.dart' as http;
void main() async {
List currencies = await getCurrencies();
print(currencies);
runApp(new MyApp(currencies));
}
class MyApp extends StatelessWidget {
final List currencies;
MyApp(this.currencies);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: new ThemeData(primarySwatch: Colors.yellow),
home: new HomePage(currencies),
);
}
}
Future<List> getCurrencies() async{
String apiUrl = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
http.Response response = await http.get(apiUrl);
return jsonDecode(response.body);
}
  1. 主页.dart
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
final List currencies;
HomePage(this.currencies);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final List<MaterialColor> colors =[Colors.green, Colors.blue, Colors.red];
List currencies = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text("Crypto Currency")
),
body: cryptoWidget(),
);
}
Widget cryptoWidget(){
return Container(
child: new Column(
children: <Widget>[
new Flexible(
child: new ListView.builder(
itemCount: widget.currencies.length,
itemBuilder: (BuildContext context, int index){
final Map currency = widget.currencies[index];
final MaterialColor color = colors[index % colors.length]; 
return getListItemUI(currency, color);
},
),
),],
),
);
}
ListTile getListItemUI(Map currency, MaterialColor color){
return new ListTile(
leading: CircleAvatar(
backgroundColor: color,
child: Text(currency['name'][0]),
),
title: Text(currency['name'],
style: TextStyle(
fontWeight: FontWeight.bold)),
subtitle: getSubtitleText(
currency['price_usd'], currency['percent_change_1h']),
isThreeLine: true,   
);
}
Widget getSubtitleText(String priceUSD, String percentChange){
TextSpan priceTextWidget = TextSpan(
text: "$$priceUSDn", style: TextStyle(color: Colors.black)
);
String percentageChange = "1 hour $percentChange%";
TextSpan percentageChangeText;
if(double.parse(percentChange)>0){
//TODO 1: Select That text
priceTextWidget = new TextSpan(
text: percentageChange, 
//TODO 2: Change its color to green
style: TextStyle(color: Colors.green));
}
else{
//TODO 1: Select That text
priceTextWidget = new TextSpan(
text: percentageChange, 
//TODO 2: Change its color to green
style: TextStyle(color: Colors.red));
}
return RichText(
text: TextSpan(
children: [priceTextWidget, percentageChangeText]));
}
}

您将percentageChangeText放在TextSpan的子级中,但percentageChangeText为null。

TextSpan percentageChangeText;
// ...
return RichText(
text: TextSpan(
children: [priceTextWidget, percentageChangeText]));

相关内容

  • 没有找到相关文章

最新更新