Я новичок, чтобы трепетать и учиться на YouTube. Я пытался создать приложение для криптовалюты, которое будет отображать цену и имя криптовалюты, которая получает данные из https://api.coinmarketcap.com/v1/ticker/?limit=50. Я написал код без ошибок компиляции.
VS CODE не дал мне ошибки. Но когда я запускаю приложение в Интернете и 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]));
}
}