0

When you switch back to a tab you get a black screen, does anyone know why?

Check on DartPad

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatelessWidget(),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      initialIndex: 1,
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('TabBar Widget'),
          bottom: const TabBar(
            tabs: <Widget>[
              Tab(
                icon: Icon(Icons.cloud_outlined),
              ),
              Tab(
                icon: Icon(Icons.beach_access_sharp),
              ),
              Tab(
                icon: Icon(Icons.brightness_5_sharp),
              ),
            ],
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            StreamBuilder(
              stream: Stream.value(true),
              builder: (context, snapshot) {
                return const Center(child: Text("It's cloudy here"));
              },
            ),
            StreamBuilder(
              stream: Stream.value(true),
              builder: (context, snapshot) {
                return const Center(child: Text("It's rainy here"));
              },
            ),
            StreamBuilder(
              stream: Stream.value(true),
              builder: (context, snapshot) {
                return const Center(child: Text("It's sunny here"));
              },
            ),
          ],
        ),
      ),
    );
  }
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Kahou
  • 3,200
  • 1
  • 14
  • 21

1 Answers1

1

I think this similar question while both throw the same error

Bad state: Stream has already been listened to

You can find there some good explanation about this issue.

As for this question, you can use initialData: true, instead of using stream.

StreamBuilder(
  initialData: true,
  //...
),

If you can also use StreamController with adding broadcast like StreamController<T>.broadcast().

You can follow previous question.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56