作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我将值从主页传递到源页面时,它显示一个错误:无法将参数类型“Future”分配给参数类型“void Function()”。 (argument_type_not_assignable 在 [ 强文本 ]
lib\home.dart:15)
我哪里做错了??
主页 -
import 'package:flutter/material.dart';
import 'sourceScreen.dart';
class Home extends StatefulWidget {
int value;
Home({this.value});
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FlatButton(
onPressed: Navigator.push(context, MaterialPageRoute(builder: (context)=>SourceScreen({value:value}))), child: null,
),
),
);
}
}
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'models/model.dart';
import 'models/card.dart';
import 'article.dart';
final API_KEY = '***';
Future<List<Source>> fetchNewsSource() async {
final response = await http.get(
'https://newsapi.org/v2/sources?language=en&country=in&apiKey=$API_KEY');
if (response.statusCode == 200) {
List sources = json.decode(response.body)['sources'];
return sources.map((source) => new Source.formJson(source)).toList();
} else {
throw Exception('Fail to load data');
}
}
class SourceScreen extends StatefulWidget {
@override
_SourceScreenState createState() => _SourceScreenState();
}
class _SourceScreenState extends State<SourceScreen> {
var list_source;
var refreshKey = GlobalKey<RefreshIndicatorState>();
@override
void initState() {
super.initState();
refreshListSource();
}
Future<Null> refreshListSource() async {
refreshKey.currentState?.show(atTop: false);
setState(() {
list_source = fetchNewsSource();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
appBar: AppBar(
elevation: 1.0,
backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
title: Text('uTTerNews'),
),
body: Center(
child: RefreshIndicator(
child: FutureBuilder<List<Source>>(
future: list_source,
builder: (context, snapshot) {
if (snapshot.hasError) {
Text('Error: ${snapshot.error}');
} else if (snapshot.hasData) {
List<Source> sources = snapshot.data;
return new ListView(
children: sources
.map((source) =>
GestureDetector(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) =>
articleScreen(source: source,)));
},
child: card(source),
))
.toList());
}
return CircularProgressIndicator();
},
),
onRefresh: refreshListSource),
),
),
);
}
}
最佳答案
代替
onPressed: Navigator.push(...)
和
onPressed: () => Navigator.push(...)
await
关键字,你可以做
onPressed: () async {
await Navigator.push(...);
await anyOtherMethod();
}
关于flutter - 错误 : The argument type 'Future' can't be assigned to the parameter type 'void Function()' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57761489/
我是一名优秀的程序员,十分优秀!