Flutter函数调用未按预期运行。 (几乎可以肯定是语法错误)

问题描述 投票:0回答:2

首先,请不要尝试告诉我从该程序中删除多余的功能!

它们专门用来向人们显示这个问题,回答我正在创建的真实程序的流程,这是对所收到错误的最小复制。

[请仅提供正确的语法,或说明您为何无法以我尝试的方式进行交流,或有什么必要。

好,所以我知道有很多方法可以使用多种编码语言,但是在这种特殊情况下,我有点受阻。我被迫将代码的各个部分移到本机类之外,但这引起了问题,因为我无法再调用函数,或者至少不能以相同的方式调用,而我似乎在任何地方都找不到正确的语法。

代码的最小可复制变体如下(使用基本的flutter程序。]

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();
  }

}

而且我知道,就此类文本而言,这太复杂了,但是我无法以一种在单个类下起作用来共享这些函数调用的方式来重构当前程序,因此该程序会重新简单地产生错误。

function flutter syntax
2个回答
0
投票

您不需要这个:

void nothing() {
    stateModify().newState();   // <------ THIS IS THE ERROR. What is the correct syntax for this line?
  }

或此:

Future<void> stateModify() {


  void newState() {
    _MyHomePageState()._incrementCounter();
  }

}

方法:

void _incrementCounter() {
    setState(() {
      _counter++;
    });

已经很严格了,就放

floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,

SetState()将触发受变量自动影响的有状态小部件。

这也是抖动的默认示例:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @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++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 
    );
  }
}

0
投票

公平,我在手机上,没有向右滚动。道歉。我不清楚您要使用stateModify实现的目标,这显然不存在。请提供更多细节。

© www.soinside.com 2019 - 2024. All rights reserved.