Skip to content

Future signal refresh/reload state is still loading even when the data is returned #394

@thangmoxielabs

Description

@thangmoxielabs

Here is the code

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

void main() {
  runApp(
    const MaterialApp(
      home: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    print('loading ${currentLocation.value.isLoading}');
    return Scaffold(
      appBar: AppBar(
        title: const Text('Signals Example'),
        actions: [
          IconButton(
            onPressed: () {
              currentLocation.reload();
            },
            icon: const Icon(Icons.refresh),
          ),
        ],
      ),
      body: Center(
        child: currentLocation.watch(context).map(
              data: (value) => Text(value.toString()),
              error: (error) => Text(error.toString()),
              loading: () => const CircularProgressIndicator(),
            ),
      ),
    );
  }
}

final currentLocation = futureSignal<LatLng?>(() async {
  await Future.delayed(Durations.short4);
  return LatLng(40.73061, -73.9);
}, debugLabel: 'currentLocation');

class LatLng {
  final double latitude;
  final double longitude;

  LatLng(this.latitude, this.longitude);

  @override
  String toString() {
    return 'LatLng(latitude: $latitude, longitude: $longitude)';
  }

  // @override
  // bool operator ==(Object other) {
  //   if (identical(this, other)) return true;

  //   return other is LatLng &&
  //       other.latitude == latitude &&
  //       other.longitude == longitude;
  // }

  // @override
  // int get hashCode => latitude.hashCode ^ longitude.hashCode;
}

The snippet above when running, the console will print

loading true
loading false

when starting the app, and whenever the reload button is pressed. However, when uncommenting the operator and hashCode on the LatLng class, after pressing the reload button, the console will print loading true, and nothing more on the next presses. Same behavior if we replace the LatLng with primitive data like int

Expectation: The asyncState.isLoading should be false after the data is being reloaded, whether the new data is equal to the old one or not

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions