简体   繁体   中英

Flutter setState 3 positional argument(s) expected, but 1 found error

on Button click, I trying to call setState but VScode generates a bug like below. Its Stateful widget and VScode suggested that setState(mounted, setState, fn);

1

 Row(
     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
     children: [
                    IconButton(
                      icon: Icon(
                        Icons.rounded_corner,
                      ),
                      onPressed: () {
                        setState(() {
                          selectedStock--;
                        });
                      },
                    ),
                    Text(selectedStock.toString()),
                    IconButton(
                      icon: Icon(
                        Icons.rounded_corner,
                      ),
                      onPressed: () {
                        // setState(mounted, setState, fn);
                        setState(() {
                          selectedStock++;
                        });
                      },
                    ),
                  ],
                ),

I had the same issue. It turned out that the problem was this import:

import 'package:html_editor_enhanced/utils/utils.dart';

It defines the following method:

void setState(
    bool mounted, void Function(Function()) setState, void Function() fn) {
  if (mounted) {
    setState.call(fn);
  }
}

which conflicts with the one defined in Flutter framework file.

In my case it turned out I didn't even need that import so I just deleted it. If you do need this import you can give it a prefix:

import 'package:html_editor_enhanced/utils/utils.dart' as utils;

This way you will call it's methods through the prefix and call Flutters setState() directly.

More info on import prefixes: https://dart.dev/guides/language/language-tour#libraries-and-visibility

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