Reputation: 158
First please do not try and tell me to remove the extraneous functions from this program.
They are specifically there to show people answering this question the flow of the real program I am creating, and this is a minimal-reproduction of the error I am receiving.
Please provide the correct syntax only, or an explanation of why you cannot communicate in the manner I am attempting, or what would be necessary.
Okay, so I know there are many ways to do things with many coding languages, however, in this particular instance I'm kinda stymied. I've been forced to move various parts of my code outside of my native class, but this is causing issues, as I can no longer call the functions I was able to previously, or at least, not in the same manner, and I can't seem to find the correct syntax anywhere.
A minimally-reproducible variant of the code as follows (Using the base flutter program.)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
void nothing() {
stateModify().newState(); // <------ THIS IS THE ERROR. What is the correct syntax for this line?
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: nothing,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Future<void> stateModify() {
void newState() {
_MyHomePageState()._incrementCounter();
}
}
And I'm aware that this is overly complex for the purpose of this kind of a text, but I cannot refactor my current program in a way that functions under a single class to share these function-calls, and this program re-produces the error simply.
Upvotes: 0
Views: 199
Reputation: 1962
There are a couple of issue with the line of code that you have highlighted. The first issue is the stateModify
function returns a Future
and the method newState
isn't defined for the type Future
. The second issue is that as far as I am aware you cannot call a function inside of a function from an external location in the Dart programming language as it looks like you are attempting to do. I appreciate that you are very limited in the refactorings that you can make to your application, however I have proposed a number of potential solutions that you could use in the below examples:
void nothing() {
StateModify.newState();
}
class StateModify {
static void newState() {
_MyHomePageState()._incrementCounter();
}
}
Future<void> nothing() async {
await stateModify();
newState();
}
Future<void> stateModify() {
}
void newState() {
_MyHomePageState()._incrementCounter();
}
Future<void> nothing() async {
(await stateModify())();
}
Future<void Function()> stateModify() async {
return () {
_MyHomePageState()._incrementCounter();
};
}
Upvotes: 1