簡體   English   中英

嘗試添加像“動態”這樣的顯式類型,或在您的分析中啟用隱式動態

[英]Try adding an explicit type like 'dynamic', or enable implicit-dynamic in your analysis

我得到的錯誤是

缺少泛型類型“MaterialPageRoute”的類型參數。 嘗試添加像“動態”這樣的顯式類型,或在您的分析選項文件中啟用隱式動態。

我正在嘗試導航到另一個頁面,但在 PUSH 和 MATERIALPAGEROUTE 處出現錯誤。 當我將光標放在 For push 上時顯示以下幾行 - 我嘗試了典型用法但沒有用

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>並且此解決方案轉到任何其他顯式類型,例如“動態”,您可能會遇到錯誤。 回復晚了非常抱歉。 希望這對其他人有幫助

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();
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM