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),
),
)
],
),
],
),
);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…