Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch can not react to lazy signal being initialiazed #310

Open
Solido opened this issue Sep 3, 2024 · 6 comments
Open

Watch can not react to lazy signal being initialiazed #310

Solido opened this issue Sep 3, 2024 · 6 comments

Comments

@Solido
Copy link
Collaborator

Solido commented Sep 3, 2024

Initializing a lazy signal will not trigger a Widget rebuild as .value maybe out of the logic execution (if condition) as isInitialized bar it.

We need the way to register signal() under Watch(...) without triggering a non initialized error.

Thanks!

Quick code for you to reproduce

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:signals_flutter/signals_flutter.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final signal = lazySignal<int>();

  @override
  build(_) => Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                'You have pushed the button this many times:',
              ),
              Watch((_) => Text(
                    signal.isInitialized ? '${signal()}' : '?',
                    style: Theme.of(context).textTheme.headlineMedium,
                  )),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            signal.value = Random().nextInt(200);
            print(signal.peek());
          },
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      );
}
@Solido Solido changed the title Watch does not react to lazy signal being initialiazed Watch can not react to lazy signal being initialiazed Sep 3, 2024
@rodydavis
Copy link
Owner

rodydavis commented Sep 3, 2024

This is interesting! I will see if I can find a way to trigger a subscription.

But it also may be a works as expected too. Will do some research!

@Solido
Copy link
Collaborator Author

Solido commented Sep 3, 2024

We may have something like isInitialized(), a method not a getter, that trigger a soft registration under Watch

I'm too busy using signals.dart but I'm curious how react, angulars and other deal with such case :)

@Solido
Copy link
Collaborator Author

Solido commented Sep 3, 2024

Another approach would be a Lazy widget that act like Visibility
Child act like a wrapped Watch

Lazy( mySignal, 
  unset: Text('not cooked'),
  child: Text(mySignal());

@Solido
Copy link
Collaborator Author

Solido commented Sep 3, 2024

Keep the wonderful simple api with

Watch((_) => ...), lazies = [lazySignal1, lazySignal2])

Should be easier to implement to.

@rodydavis
Copy link
Owner

I think another approach would be to make the initialization check a signal too so it will subscribe regardless!

@Solido
Copy link
Collaborator Author

Solido commented Sep 3, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants