Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dart.core.sink in flutter

Tags:

flutter

dart

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



void main() => runApp(MyApp());

//Using Bloc
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: bloc.darkThemeEnabled,
      initialData: false,
      builder: (context, snapshot) => MaterialApp(
          theme: snapshot.data ? ThemeData.dark() : ThemeData.light(),
          home: HomePage(snapshot.data)),
    );
  }
}

class HomePage extends StatelessWidget {
  final bool darkThemeEnabled;

  HomePage(this.darkThemeEnabled);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dynamic Theme"),
      ),
      body: Center(
        child: Text("Hello World"),
      ),
      drawer: Drawer(
        child: ListView(
          children: <Widget>[
            ListTile(
              title: Text("Dark Theme"),
              trailing: Switch(
                value: darkThemeEnabled,
                onChanged: bloc.changeTheme,
              ),
            )
          ],
        ),
      ),
    );
  }
}


class Bloc {
  final _themeController = StreamController<bool>();
  get changeTheme => _themeController.sink.add;
  get darkThemeEnabled => _themeController.stream;
}

final bloc = Bloc();

1.A warning says to Close instances of dart.core.sink 2.Why dart.core.sink is used in flutter? 3.How can I solve this error 4.Its error documentation redirects me to this website link 5.I don't know how to use these methods in flutter please guide me

like image 590
abin Avatar asked Sep 30 '19 13:09

abin


2 Answers

dart.core.sink is an interface that is implemented by Stream. The warning is showing, because the dart compiler wants you to .close() your instance of a Stream. In this case that is your final _themeController = StreamController<bool>().

If you want to fix the warning, add

void dispose() {
  _themeController.close();
}

to your Bloc class.

Just adding the method is not doing much, since it's not called. So you should change your main() method to call bloc.dispose() after runApp(MyApp()).

like image 169
jnnks Avatar answered Oct 05 '22 13:10

jnnks


That error occur when missing close StreamController. Simple way to fix:

  • Create abstract class:

    abstract class Bloc { void dispose(); }

  • Your bloc class implements Bloc, now you can close StreamController in dispose:

      class ColorBloc implements Bloc {
    
      // streams of Color
      StreamController streamListController = StreamController<Color>.broadcast();
    
      // sink
      Sink get colorSink => streamListController.sink;
    
      // stream
      Stream<Color> get colorStream => streamListController.stream;
    
      // function to change the color
      changeColor() {
          colorSink.add(getRandomColor());
      }
    
      // Random Colour generator
      Color getRandomColor() {
          Random _random = Random();
          return Color.fromARGB(
              _random.nextInt(256),
              _random.nextInt(256),
              _random.nextInt(256),
              _random.nextInt(256),
          );
      }
    
      // close Stream
      @override
      void dispose() {
          streamListController.close();
      }
    

    }

like image 41
O Thạnh Ldt Avatar answered Oct 05 '22 12:10

O Thạnh Ldt