'Set<Widget>' 不能分配给参数类型 'Widget' [供应商]

问题描述

在消费者(提供者:^4.3.2+3)中包装文本小部件时,编译器给出以下错误-

The argument type 'Set<Widget>' can't be assigned to the parameter type 'Widget'.  

从 Set 中删除返回类型 <Widget> 时,会出现以下错误 -

The argument type 'Set<StatelessWidget>' can't be assigned to the parameter type 'Widget'.

如何解决这个问题?

图片

Image

代码

import 'package:Flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    // Provide the model to all widgets within the app. We're using
    // ChangeNotifierProvider because that's a simple way to rebuild
    // widgets when a model changes. We Could also just use
    // Provider,but then we would have to listen to Counter ourselves.
    //
    // Read Provider's docs to learn about all the available providers.
    ChangeNotifierProvider(
      // Initialize the model in the builder. That way,Provider
      // can own Counter's lifecycle,making sure to call `dispose`
      // when not needed anymore.
      create: (context) => Counter(),child: MyApp(),),);
}

/// Simplest possible model,with just one field.
///
/// [ChangeNotifier] is a class in `Flutter:foundation`. [Counter] does
/// _not_ depend on Provider.
class Counter with ChangeNotifier {
  int value = 0;

  void increment() {
    value += 1;
    notifyListeners();
  }

  void decrement() {
    value -= 1;
    notifyListeners();
  }

}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',theme: ThemeData(
        primarySwatch: Colors.blue,home: MyHomePage(),);
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),body: Column(
              mainAxisAlignment: MainAxisAlignment.center,children :<Widget>[
                Container(
                  padding: EdgeInsets.all(300),color: Colors.blue,//Error: The argument type 'Set<Widget>' can't be assigned to the parameter type 'Widget'.
                  child: <Widget> {
                    Text('You have pushed the button this many times:'),Consumer<Counter>(
                      builder: (context,counter,child) =>
                          Text(
                            '${counter.value}',style: Theme.of(context).textTheme.headline4,},Row(
                    children:<Widget> [
                     Align(
                      alignment: Alignment.bottomright,child: FloatingActionButton(
                      onpressed: () {
                        var counter = context.read<Counter>();
                        print('${counter.value} = counter');
                        counter.increment();
                      },tooltip: 'Increment',child: Icon(Icons.add),Align(
                      alignment: Alignment.bottomLeft,child: FloatingActionButton(
                      onpressed: () {
                        var counter = context.read<Counter>();
                        print('${counter.value} = counter');
                        counter.decrement();
                      },tooltip: 'Decrement',child: Icon(Icons.minimize),)
                  ],],);
}
}

解决方法

您可以在下面复制粘贴运行完整代码
您可以使用 Column(children: <Widget>[ 而不是 <Widget>{
代码片段

Container(
    padding: EdgeInsets.all(30),color: Colors.blue,child: Column(
      children: <Widget>[
        Text('You have pushed the button this many times:'),

工作演示

enter image description here

完整代码

    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';

    void main() {
      runApp(
        // Provide the model to all widgets within the app. We're using
        // ChangeNotifierProvider because that's a simple way to rebuild
        // widgets when a model changes. We could also just use
        // Provider,but then we would have to listen to Counter ourselves.
        //
        // Read Provider's docs to learn about all the available providers.
        ChangeNotifierProvider(
          // Initialize the model in the builder. That way,Provider
          // can own Counter's lifecycle,making sure to call `dispose`
          // when not needed anymore.
          create: (context) => Counter(),child: MyApp(),),);
    }

    /// Simplest possible model,with just one field.
    ///
    /// [ChangeNotifier] is a class in `flutter:foundation`. [Counter] does
    /// _not_ depend on Provider.
    class Counter with ChangeNotifier {
      int value = 0;

      void increment() {
        value += 1;
        notifyListeners();
      }

      void decrement() {
        value -= 1;
        notifyListeners();
      }
    }

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',theme: ThemeData(
            primarySwatch: Colors.blue,home: MyHomePage(),);
      }
    }

    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Flutter Demo Home Page'),body: Column(
            mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
              Container(
                padding: EdgeInsets.all(30),child: Column(
                  children: <Widget>[
                    Text('You have pushed the button this many times:'),Consumer<Counter>(
                      builder: (context,counter,child) => Text(
                        '${counter.value}',style: Theme.of(context).textTheme.headline4,],Row(
                children: <Widget>[
                  Align(
                    alignment: Alignment.bottomRight,child: FloatingActionButton(
                      onPressed: () {
                        var counter = context.read<Counter>();
                        print('${counter.value} = counter');
                        counter.increment();
                      },tooltip: 'Increment',child: Icon(Icons.add),Align(
                    alignment: Alignment.bottomLeft,child: FloatingActionButton(
                      onPressed: () {
                        var counter = context.read<Counter>();
                        print('${counter.value} = counter');
                        counter.decrement();
                      },tooltip: 'Decrement',child: Icon(Icons.minimize),)
                ],);
      }
    }