flutter NoSuchMethodError was thrown building MyAppMain(dirty):

Multi tool use
Multi tool use


flutter NoSuchMethodError was thrown building MyAppMain(dirty):



I'm trying to implement the inherited widget but getting error as context got dirty for MyAppMain().


MyAppMain()



I/flutter (16558): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
(16558): The following NoSuchMethodError was thrown building
MyAppMain(dirty): I/flutter (16558): The getter 'mycounter' was called
on null. I/flutter (16558): Receiver: null I/flutter (16558): Tried
calling: mycounter


import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
int counter;
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(

primarySwatch: Colors.blue,
),
home:Scaffold(
appBar: new AppBar(

title: new Text('My App'),
),
body: MyAppMain(),
floatingActionButton: new CounterWidget(
),
),
);
}
}
class MyAppMain extends StatelessWidget {
@override
Widget build(BuildContext context) {
int _counter = 0;
int myinherited = MyInherited.of(context).mycounter;

_counter = myinherited+ 1;
return new Center(

child: new Column(

mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),

new Text(
'${_counter}',
style: Theme
.of(context)
.textTheme
.display1,
),
],
),
);
}

MyAppMain();
}

class CounterWidget extends StatefulWidget {


CounterWidget();

@override
MyInheritedState createState() => new MyInheritedState();

static MyInheritedState of(BuildContext context) {
return (context.inheritFromWidgetOfExactType(MyInherited) as MyInherited)
.data;
}
}

class MyInheritedState extends State<CounterWidget> {
String _myField;

// only expose a getter to prevent bad usage
String get myField => _myField;

int mycounter=0;

void onMyFieldChange(String newValue) {
setState(() {
_myField = newValue;
});
}

@override
Widget build(BuildContext context) {
return new MyInherited(
data: this,
mycounter:mycounter,
child: RaisedButton(child: new Text('click'), onPressed: () {
setState(() {
mycounter++;
});
},),
);
}
}

class MyInherited extends InheritedWidget {
final MyInheritedState data;
final int mycounter;
MyInherited({Key key, this.mycounter,this.data, Widget child})
: super(key: key, child: child);
static MyInherited of(BuildContext context){
return context.inheritFromWidgetOfExactType(MyInherited );
}
@override
bool updateShouldNotify(MyInherited old) {
return true;
}
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

r bMnmSXG97xR I Dv4,25SKKz Ljy e7ahiw5A LS
u1O8iroNk,kgSVU97gH,4yl3RZBnzQgZXnME6 vV

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications