我得到的错误是
通用类型“MaterialPageRoute”的类型参数丢失。
尝试添加显式类型(例如“动态”),或在分析选项文件中启用隐式动态。
我正在尝试导航到另一个页面,但在PUSH和MATERIALPAGEROUTE遇到错误。
当我将光标放在
推送-
我尝试了典型用法但没有用
Future<T> push<T extends Object>(BuildContext context, Route<T> route)
package:flutter/src/widgets/navigator.dart
Push the given route onto the navigator that most tightly encloses the given context. The new route and the previous route (if any) are notified (see [Route.didPush] and [Route.didChangeNext]). If the [Navigator] has any [Navigator.observers], they will be notified as well (see [NavigatorObserver.didPush]).
Ongoing gestures within the current route are canceled when a new route is pushed.
Returns a [Future] that completes to the result value passed to [pop] when the pushed route is popped off the navigator.
The T type argument is the type of the return value of the route.
Typical usage is as follows:
void _openMyPage() {
Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => MyPage()));
}
Missing type arguments for generic method 'push<T extends Object>'.
Try adding an explicit type like 'dynamic', or enable implicit-dynamic in your analysis options file.dart(implicit_dynamic_method)
对于MaterialPageRoute
(new) MaterialPageRoute<dynamic> MaterialPageRoute({Widget Function(BuildContext) builder}, {RouteSettings settings}, {bool maintainState}, {bool fullscreenDialog})
package:flutter/src/material/page.dart
Construct a MaterialPageRoute whose contents are defined by [builder].
The values of [builder], [maintainState], and [fullScreenDialog] must not be null.
Specify type annotations.dart(always_specify_types)
Missing type arguments for generic type 'MaterialPageRoute<dynamic>'.
Try adding an explicit type like 'dynamic', or enable implicit-dynamic in your analysis options file.dart(implicit_dynamic_type)
以下是我遇到错误的代码
import 'package:app/pages/product.dart';
import 'package:flutter/material.dart';
class Products extends StatelessWidget {
final List<String> products;
Products([this.products = const []]) {
print('[Products Widget] Constructor');
}
Widget _buildProductItem(BuildContext context, int index) {
return Card(
child: Column(
children: <Widget>[
Image.asset('assets/assets.jpg'),
Text(products[index]),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text('Details'),
onPressed :() => Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => ProductPage(),
),
),
),
],
)
],
),
);
}
Widget _buildProductList() {
Widget productCard;
if (products.length > 0) {
productCard = ListView.builder(
itemBuilder: _buildProductItem,
itemCount: products.length,
);
} else {
productCard = Container();
}
return productCard;
}
@override
Widget build(BuildContext context) {
print('[Products Widget] build()');
return _buildProductList();
}
}
请帮我解决这个错误
最佳答案
这应该可以完成,您需要做的就是在推送后添加<MaterialPageRoute>
,此解决方案将转到其他任何显式类型,例如“dynamic”,错误。回复晚了非常抱歉。希望这对别人有帮助
import 'package:app/pages/product.dart';
import 'package:flutter/material.dart';
class Products extends StatelessWidget {
final List<String> products;
Products([this.products = const []]) {
print('[Products Widget] Constructor');
}
Widget _buildProductItem(BuildContext context, int index) {
return Card(
child: Column(
children: <Widget>[
Image.asset('assets/assets.jpg'),
Text(products[index]),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text('Details'),
onPressed :() => Navigator.push<MaterialPageRoute>(
context,
MaterialPageRoute(
builder: (BuildContext context) => ProductPage(),
),
),
),
],
)
],
),
);
}
Widget _buildProductList() {
Widget productCard;
if (products.length > 0) {
productCard = ListView.builder(
itemBuilder: _buildProductItem,
itemCount: products.length,
);
} else {
productCard = Container();
}
return productCard;
}
@override
Widget build(BuildContext context) {
print('[Products Widget] build()');
return _buildProductList();
}
}
关于flutter - 尝试添加像 'dynamic'这样的显式类型,或在分析中启用隐式动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60798549/