简体   繁体   中英

'Set<Widget>' can't be assigned to the parameter type 'Widget'. [Provider]

On wrapping Text widget in a Consumer(Provider: ^4.3.2+3),the compiler gives the below error-

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

On removing return type <Widget> from the Set, gives the below error -

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

How to resolve this?

Image:

图片

Code:

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),
                    ),
                   )
                  ],

                 ),
              ],
      ),
    );
}
}

You can copy paste run full code below
You can use Column(children: <Widget>[ not <Widget>{
code snippet

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

working demo

在此处输入图像描述

full code

    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),
                color: Colors.blue,
                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),
                    ),
                  )
                ],
              ),
            ],
          ),
        );
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM