简体   繁体   中英

Flutter GetX multi controller not work in the same widget

CODE:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    _counter++;
  }

  @override
  Widget build(BuildContext context) {
    TestInt testInt2 = TestInt(100);
    TestInt testInt1 = TestInt(0);

    Get.put<TestInt>(testInt2, tag: "testInt2");
    Get.put<TestInt>(testInt1, tag: "testInt1");
    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(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times: $_counter',
            ),
            GetBuilder<TestInt>(
              init: Get.find<TestInt>(tag: "testInt2"),
              builder: (_) {
                return Text(
                  '${Get.find<TestInt>(tag: "testInt2").number}',
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),
            GetBuilder<TestInt>(
              init: Get.find<TestInt>(tag: "testInt1"),
              builder: (_) {
                return Text(
                  '${Get.find<TestInt>(tag: "testInt1").number}',
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),
          ],
        ),
      ),
      persistentFooterButtons: [
        FloatingActionButton(
          onPressed: () {
            _incrementCounter();
            Get.find<TestInt>(tag: "testInt2").increment();
          },
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
        FloatingActionButton(
          onPressed: () {
            _incrementCounter();
            Get.find<TestInt>(tag: "testInt1").increment();
          },
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      ], // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class TestInt extends GetxController {
  late int number;

  TestInt(number) {
    this.number = number;
  }

  void increment() {
    this.number = number + 1;
    update();
  }
}

Summary:

On the code above, I made 2 floating buttons and 2 GetBuilder to show testInt1's number and testInt2's number separately.

But Only one button works well and the other button doesn't work.

I think it might works only first initiation with init: Get.find<TestInt>(tag: "testInt2"), and second init: Get.find<TestInt>(tag: "testInt1"), does not works..

Question: How to make both works?

Set global:false to your GetBuilder.

GetBuilder<TestInt>(
              init: Get.find<TestInt>(tag: "testInt2"),
              global: false,
              builder: (_) {
                return Text(
                  '${Get.find<TestInt>(tag: "testInt2").number}',
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),

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