![](/img/trans.png)
[英]The argument type 'Future<void> Function()' can't be assigned to the parameter type 'Future<bool> Function()?
[英]error: The argument type 'Future' can't be assigned to the parameter type 'void Function()'
當我將值從主頁傳遞到源頁面時,它顯示錯誤:無法將參數類型“Future”分配給參數類型“void Function()”。 (argument_type_not_assignable 在 [ strong text ] 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();
}
這個在最新版本中對我有用
onPressed: () {
selectHandler();
},
@CopsOnRoad -- Here is the sample code that is working for me.
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.grey,
textColor: Colors.black,
child: Text('Answer 1'),
**onPressed: () => selectHandler()**,
),
);
}
}
只需將 final Function selectHandler 更改為 final VoidCallback selectHandler;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.