Flutter - NoSuchMethodError:方法'visitChildren'在null上被调用
Posted
技术标签:
【中文标题】Flutter - NoSuchMethodError:方法\'visitChildren\'在null上被调用【英文标题】:Flutter - NoSuchMethodError: The method 'visitChildren' was called on nullFlutter - NoSuchMethodError:方法'visitChildren'在null上被调用 【发布时间】: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]));
【问题讨论】:
【参考方案1】:您将 percentageChangeText
放入 TextSpan 的子项中,但 percentageChangeText
为空。
TextSpan percentageChangeText;
// ...
return RichText(
text: TextSpan(
children: [priceTextWidget, percentageChangeText]));
【讨论】:
非常感谢您的回复。整理好了!以上是关于Flutter - NoSuchMethodError:方法'visitChildren'在null上被调用的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 13 和 Flutter 2.5.1 - 致命错误:找不到“Flutter/Flutter.h”文件 #import <Flutter/Flutter.h>
Flutter 致命错误:找不到“Flutter/Flutter.h”文件
[Flutter] flutter项目一直卡在 Running Gradle task 'assembleDebug'...
flutter 日志输出,Flutter打印日志,flutter log,flutter 真机日志
Flutter开发 Flutter 包和插件 ( Flutter 包和插件简介 | 创建 Flutter 插件 | 创建 Dart 包 )