Home > Enterprise >  Error: Field '_connectionChangeStream' should be initialized because its type 'Stream
Error: Field '_connectionChangeStream' should be initialized because its type 'Stream

Time:12-10

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:nartaqi/no_connection_page.dart';
import 'package:nartaqi/notifications.dart';

import 'body.dart';
import 'connectionStatusSingleton.dart';
import 'constants.dart';

class HomePage extends StatefulWidget {
   const HomePage({
    Key? key,
  }) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
  bool isOffline = false;

   StreamSubscription _connectionChangeStream;

  @override
  initState() {
    super.initState();
    ConnectionStatusSingleton connectionStatus = ConnectionStatusSingleton.getInstance();
      _connectionChangeStream = connectionStatus.connectionChange.listen(connectionChanged);
  }

  void connectionChanged(dynamic hasConnection) {
    setState(() {
      isOffline = !hasConnection;
    });
  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        backgroundColor: kPrimaryColor,
        elevation: 0.0,
        leading: IconButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => const NotificationPage(),),);
          },
          icon: const Icon(
            Icons.notifications,
            color: Color(0xFFF9A826),
          ),
        ),
        actions: const [
          Padding(
            padding:
            EdgeInsets.symmetric(horizontal: 10.0, vertical: 16.0),
            child: Text(
              'الرئيسية',
              style: kHomeScreenTextStyle,
            ),
          ),
        ],
      ),
      body: isOffline? NoConnectionPage():Body(),
    );
  }

i get an error when i initialize with late the line(StreamSubscription _connectionChangeStream;), also the _connectionChangeStream becomes gray, the value is not used anywhere, im actually trying to connect my app to the internet and check whether there is an internet connection or not, apparently the connectivity package only tells you if you are connected to a network it dose not check for internet access

CodePudding user response:

To solve this error either you have to initialize your _connectionChangeStream or you have to make it nullable as below

StreamSubscription? _connectionChangeStream;
  • Related