Quantcast
Channel: why do i get error of Undefined name even when the variable is defined? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

why do i get error of Undefined name even when the variable is defined?

$
0
0

I am getting an undefined name error even when i defined the var in my dart file,Below is the error image

enter image description here

Below is my code

import 'package:flutter/material.dart';import 'package:provider/provider.dart';import '../providers/products.dart';class ProductDetailsScreen extends StatelessWidget {  // final String title;  // ProductDetailsScreen(this.title);  static const routeName = '/product-detail';  @override  Widget build(BuildContext context) {    final productId = ModalRoute.of(context)?.settings.arguments as String?;    if (productId != null) {      final loadedProduct = Provider.of<Products>(context)          .items          .firstWhere((prod) => prod.id == productId);    }    return Scaffold(      appBar: AppBar(        title: Text(loadedProduct.title),      ),    );  }}

plus when i try to move the return code-block to the if method like this

import 'package:flutter/material.dart';import 'package:provider/provider.dart';import '../providers/products.dart';class ProductDetailsScreen extends StatelessWidget {  // final String title;  // ProductDetailsScreen(this.title);  static const routeName = '/product-detail';  @override  Widget build(BuildContext context) {    final productId = ModalRoute.of(context)?.settings.arguments as String?;    if (productId != null) {      final loadedProduct = Provider.of<Products>(context)          .items          .firstWhere((prod) => prod.id == productId);      return Scaffold(        appBar: AppBar(          title: Text(loadedProduct.title),        ),      );    }  }}

it then throws an error "the body might complete normally, causing null to be returned, but the return type, "widget" is a potentially non-nullable type.

Please i need help as it looks like my unfamiliarity with null-safety is giving me lot of bugs.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>