Giter Site home page Giter Site logo

Comments (4)

brianegan avatar brianegan commented on July 19, 2024 6

Hey there -- took a look at your code. The problem is that you're creating the Store<AppState> inside the build function of a StatelessWidget (MusicParty). As a heads up: build functions are called over and over again. In this case, Hot Reload will trigger the build function in your MusicParty after hot reload.

This means you're creating a new store each time the build method is called, and that's why you're running into trouble.

Instead, you need to create the Store exactly once: Either in the main function or in a StatefulWidget at the top of your Widget hierarchy.

Please try this as an alternative and everything should be working:

import 'package:firebaseredux/models/app_state.dart';
import 'package:firebaseredux/pages/home_page.dart';
import 'package:firebaseredux/reducers/app_reducer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:redux_logging/redux_logging.dart';

void main() => runApp(new MusicParty());

class MusicParty extends StatefulWidget {
  @override
  MusicPartyState createState() {
    return new MusicPartyState();
  }
}

class MusicPartyState extends State<MusicParty> {
  final store = Store<AppState>(
    appReducer,
    initialState: new AppState(count: 0, isLoading: false),
    middleware: [LoggingMiddleware.printer()],
  );

  @override
  Widget build(BuildContext context) {
    final title = "Me Suite";

    return new StoreProvider<AppState>(
      store: store,
      child: MaterialApp(
        title: title,
        home: HomePage(title),
      ),
    );
  }
}

from flutter_redux.

jangedoo avatar jangedoo commented on July 19, 2024

Thank you. Now everything works as expected.

from flutter_redux.

ollyde avatar ollyde commented on July 19, 2024

This will still fail with persist, load the store asnyc as the app loads with the following.

void main() async {
  runApp(MyApp(store: await AppState.createStore()));
}

from flutter_redux.

amercobra avatar amercobra commented on July 19, 2024

Hey there -- took a look at your code. The problem is that you're creating the Store<AppState> inside the build function of a StatelessWidget (MusicParty). As a heads up: build functions are called over and over again. In this case, Hot Reload will trigger the build function in your MusicParty after hot reload.

This means you're creating a new store each time the build method is called, and that's why you're running into trouble.

Instead, you need to create the Store exactly once: Either in the main function or in a StatefulWidget at the top of your Widget hierarchy.

Please try this as an alternative and everything should be working:

import 'package:firebaseredux/models/app_state.dart';
import 'package:firebaseredux/pages/home_page.dart';
import 'package:firebaseredux/reducers/app_reducer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:redux_logging/redux_logging.dart';

void main() => runApp(new MusicParty());

class MusicParty extends StatefulWidget {
  @override
  MusicPartyState createState() {
    return new MusicPartyState();
  }
}

class MusicPartyState extends State<MusicParty> {
  final store = Store<AppState>(
    appReducer,
    initialState: new AppState(count: 0, isLoading: false),
    middleware: [LoggingMiddleware.printer()],
  );

  @override
  Widget build(BuildContext context) {
    final title = "Me Suite";

    return new StoreProvider<AppState>(
      store: store,
      child: MaterialApp(
        title: title,
        home: HomePage(title),
      ),
    );
  }
}

Thank you, you saved my life!!

from flutter_redux.

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.