【发布时间】:2020-06-07 04:39:44
【问题描述】:
我是新来的颤振和从 youtube 学习的人。我试图构建一个加密货币应用程序,它将显示从https://api.coinmarketcap.com/v1/ticker/?limit=50 获取数据的加密货币的价格和名称。我写的代码没有任何编译错误。
VS CODE 没有给我任何错误。但是当我在 web 和 Android 上运行应用程序时,它会抛出一个错误,上面写着: NoSuchMethodError:方法 'visitChildren' 在 null 上被调用。 收件人:空 尝试调用: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);
}
- HomePage.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: "\$$priceUSD\n", 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]));
}
}
【问题讨论】:
标签: android flutter dart flutter-web