Giter Site home page Giter Site logo

Why list not updating? about flutter_hooks HOT 7 CLOSED

tony123S avatar tony123S commented on June 11, 2024
Why list not updating?

from flutter_hooks.

Comments (7)

soxft avatar soxft commented on June 11, 2024 1

I have the same questions, useValueListenable is still not working for me when using xx.value.add(1)
but xx.value = [...xx.value, 1] works

here is the code

class CampusMerchantHome extends HookWidget {
  const CampusMerchantHome({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final devices = useState<List<int>>([]);

    Future<void> initPrinter() async {
      devices.value.add(1);
    }

    Widget deviceItem(ValueNotifier<List<int>> device) {
      final deviceListener = useValueListenable(device);

      return ListView.builder(
        itemCount: deviceListener.length,
        itemBuilder: (context, index) {
          final deviceChoose = deviceListener[index];

          return ListTile(
            title: Text(deviceChoose.toString()),
          );
        },
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text("welcome to use 校悦送"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () => initPrinter(),
              child: const Text('initPrinter'),
            ),
            Expanded(
              child: deviceItem(devices),
            ),
          ],
        ),
      ),
    );
  }
}

from flutter_hooks.

rrousselGit avatar rrousselGit commented on June 11, 2024

You're not listening to the ValueNotifier

Use useValueListenable(list);

from flutter_hooks.

tony123S avatar tony123S commented on June 11, 2024

You're not listening to the ValueNotifier

Use useValueListenable(list);

Sorry, I have edited my post to make it clearer. I'm using useState in Page A.
I have added useValueListenable after list.value = [...list.value..add(onValue)];, , but still same.

from flutter_hooks.

rrousselGit avatar rrousselGit commented on June 11, 2024

My answer doesn't change. Use useValueListenable in EditScreen :)

from flutter_hooks.

tony123S avatar tony123S commented on June 11, 2024

Error after useValueListenable is added,


E/flutter (23009): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:flutter_hooks/src/framework.dart': Failed assertion: line 133 pos 12: 'HookElement._currentHookElement != null': Hooks can only be called from the build method of a widget that mix-in `Hooks`.
E/flutter (23009): 
E/flutter (23009): Hooks should only be called within the build method of a widget.
E/flutter (23009): Calling them outside of build method leads to an unstable state and is therefore prohibited.
E/flutter (23009): 
E/flutter (23009): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
E/flutter (23009): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
E/flutter (23009): #2      Hook.use (package:flutter_hooks/src/framework.dart:133:12)
E/flutter (23009): #3      use (package:flutter_hooks/src/framework.dart:18:32)
E/flutter (23009): #4      useValueListenable (package:flutter_hooks/src/listenable.dart:9:3)
E/flutter (23009): #5      EditScreen.build.<anonymous closure>.<anonymous closure> (package:xxx/edit_screen.dart:23:17)
E/flutter (23009): <asynchronous suspension>

Am I added it in the right place?

from flutter_hooks.

tony123S avatar tony123S commented on June 11, 2024

fixed.

Widget _showBody(ValueNotifier<List<VehicleItems>> list) {
  final vehicleList = useValueListenable(list); // useValueListenable put here instead
  return ListView.separated(
      padding: EdgeInsets.zero,
      shrinkWrap: true,
      separatorBuilder: (context, index) => Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20),
          child: Container(
            color: Colors.grey.shade200,
            height: 1,
          )),
      itemCount: vehicleList.length,
      itemBuilder: (context, index) {
        final items = vehicleList[index];
        return InkWell(
            onTap: () {},
            child: ListItemPadding(
              child: VehicleItem(items),
              // isDeleteVisible: false,
            ));
      });
}

from flutter_hooks.

shahriarsihamstat avatar shahriarsihamstat commented on June 11, 2024

I have the same questions, useValueListenable is still not working for me when using xx.value.add(1) but xx.value = [...xx.value, 1] works

@soxft I had the same problem, and your solution worked for me as well. Kudos!

from flutter_hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.