'Flutter: How to solve this type 'Null' is not a subtype of type 'num'?

I'm already using the null safety in my code but unfortunately it's not working and returning type 'Null' is not a subtype of type 'num' and i don't know why! i have checked everything and made sure that everything is working in a perfect way which it was until i have signed a parameter (tankID) to all widgets then this happened :'(.

Here is my code:

import 'package:flutter/material.dart';
import 'package:flutter_locales/flutter_locales.dart';
import 'package:provider/provider.dart';
import 'package:smart_tank1/provider/project_provider.dart';


class SummaryPage extends StatefulWidget {
  final String? tanksID;
  SummaryPage({Key? key, required this.tanksID});
  @override
  _SummaryPageState createState() => _SummaryPageState();
}

class _SummaryPageState extends State<SummaryPage> {

  var _isInit = true;
  @override
  void didChangeDependencies(){
    if(_isInit){
       Provider.of<ProjectProvider>(context, listen: false).roofTankIDData('${widget.tanksID}');
    }
    _isInit = false;
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: StreamBuilder(
        stream:  Provider.of<ProjectProvider>(context, listen: false).roofTankIDData('${widget.tanksID}'),
        builder: (context, AsyncSnapshot? snapshot) {
          if(snapshot!.connectionState == ConnectionState.waiting){
            return Center(child: CircularProgressIndicator(),);
          }

          if(snapshot.hasData){
            final mainTankLitersAmount = snapshot.data!.snapshot.value['mainTankLitersAmount'];
            final mainTankQuantity = snapshot.data!.snapshot.value['mainTankQuantity'];
            final familyNumber = snapshot.data!.snapshot.value['familyMembers'];

             double? percentageNow = mainTankLitersAmount / mainTankQuantity;
             int? consLiters = mainTankQuantity - mainTankLitersAmount;

             double? consPerP = consLiters! / familyNumber;
             double? consWeek = consPerP * 7;
             double? consMonth = consPerP * 30;
             

            return Padding(
            padding: const EdgeInsets.only(bottom: 136, top: 32.0),
            child: Column(
              children: [
                 SizedBox(width: double.infinity),
                LocaleText(
                  "usageSum",
                  style: Theme.of(context).textTheme.headline4,
                ),
                SizedBox(height: 60,),
               LocaleText('mainTank'),
               SizedBox(height: 40,),
                Padding(
                      padding: const EdgeInsets.only(left: 40, right: 40),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          LocaleText('capacity'),
                          Text('$mainTankQuantity (L)')
                        ],
                      ),
                    ),
                SizedBox(height: 30,),
                Padding(
                  padding: const EdgeInsets.only(left: 40, right: 40),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleText('waterLevel'),
                      Text('${(percentageNow! * 100).toInt()}%')
                    ],
                  ),
                ),
                
                SizedBox(height: 30,),
                Padding(
                  padding: const EdgeInsets.only(left: 40, right: 40),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleText('tconsPerL'),
                      Text('$consLiters (L)')
                    ],
                  ),
                ),
                SizedBox(height: 30,),
                Padding(
                  padding: const EdgeInsets.only(left: 40, right: 40),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleText('pcons'),
                      Text('${consPerP.toInt()} (L)')
                    ],
                  ),
                ),
               SizedBox(height: 30,),
                Padding(
                  padding: const EdgeInsets.only(left: 40, right: 40),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleText('wPcons'),
                      Text('${consWeek.toInt()} (L)')
                    ],
                  ),
                ),
                SizedBox(height: 30,),
                Padding(
                  padding: const EdgeInsets.only(left: 40, right: 40),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleText('mPcons'),
                      Text('${consMonth.toInt()} (L)')
                    ],
                  ),
                ),
              ],
            ),
          );
          }
          return Center(child: Text('Something went wrong!'),);
          
        }
      ),
    );
  }
}

This is the error it returns: type 'Null' is not a subtype of type 'num' The relevant error-causing widget was StreamBuilder<DatabaseEvent?>



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source