No properties are available on my Flutter Project using Material - flutter

I am in the process of updating a Flutter project from v2 to v3 and running into some weird issues. Everywhere anything Material related is being used, i get this prop does not exist. Here are some examples:
_uiViewPort(MaterialApp(
home: StreamBuilder<bool>(
stream: _authenticationService.loginStatusChangeStream,
builder: (context, snapshot) {
if (snapshot.data == true) {
return Scaffold(
key: locator<DialogService>().scaffoldKey,
resizeToAvoidBottomInset: true,
drawer: DrawerView(),
appBar: AppBarCustom(),
body: _initializeViewPort(),
bottomNavigationBar: BottomNavigationView(),
);
}
return Scaffold(
resizeToAvoidBottomInset: true,
body: _initializeViewPort(),
bottomNavigationBar: BottomNavigationView(),
);
}),
));
I get The named parameter home isn't defined and The named parameter stream isn't defined, etc. etc.
This is the same for other fields too like:
Text(
title,
style: AppTheme.instance
.textStyleSmall(color: Colors.black, fontWeight: FontWeight.w600),
),
It tells me that style is not defined. I upgraded the project to current Flutter and am trying to remediate all the errors.
Can anyone help me out at all? It seems like Material just isn't installed. But I don't get an error when importing it at the top of the page?

Try running
flutter clean flutter pub get
if you are using android studio do file -> Invalidate cache and restart (find this at top bar)

make sure to import 'package:flutter/material.dart'; at top of your file

Related

Two scaffolds into one page because of navigation in flutter

I have an issue with the navigation process in my Flutter app.
First, I have a home page that has a button. When I press search, I go to the search page, obviously, and the results will appear on the home page again.
If no results were found, a dialog would appear that says "Try again" and has an OK button.
I want to press the OK button to navigate to the search page again. But there is a problem with the scaffolds like the image below. How can I fix this?
And my home page code, for alert dialog is like this:
Widget build(BuildContext context) {
print(widget.selectedURL);
return MaterialApp(
home: Center(
child: FutureBuilder<List<Pet>>(
future: API.get_pets(widget.selectedURL),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Pet>? pet_data = snapshot.data;
print(pet_data?.length);
return Padding(
padding: const EdgeInsets.all(8.0),
child:(pet_data?.length == 0)
? AlertDialog(
title: const Text("Failed"),
alignment: Alignment.topCenter,
content: const Text(
'No Pets Found. Please try different search options.'),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const Search())),
)
],
)
: ListView.builder(
//showing the list of results
)
The problem is you are wrapping everything inside a MaterialApp.
MaterialApp defines a Navigator property, which means it should be at the top-level of your application and it should be unique. If you have two different MaterialApp they will use their own Navigator, hence they will not share routes. See more informations here MaterialApp.
The usual design is:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: YourWidgets(
[...]
)//YourWidgets
)//Scaffold
);//MaterialApp
}
Try removing MaterialApp, or changing your MaterialApp to something else, like Material if you really need additional customization.

Flutter Navigation2.0 with named route loading bar issue

ElevatedButton(
onPressed: () => Navigator.pushNamed(
context, '/addFood'),
child: Text('I Have Food'))
The above navigation push causing the loading bar on the top. This caused after 2.5 flutter update!. Can't figure out how to resolve the issue.
PS : The loading bar appears to be new and whenever chrome loads the web page.
Looks like found out the issue!.
When I introduce scaffold, the loading bar gone away, anyway flutter team should provide this new web page loading bar.
MaterialApp(
debugShowCheckedModeBanner: true,
theme: appTheme,
home: SafeArea(
child: Scaffold(
body: BlocListener<AddfoodCubit, AddfoodState>(
listener: (context, state) {
// TODO: implement listener
},
child: CustomScrollView(
slivers: [**strong text**

'Exception : invalid image data ' error in android studio

I am learning flutter development with dart from udemy , and I am stuck in this part of the course , in this code it is supposed to display a centered image on the emulator app ( I already activated internet on the emulator and I can execute google research !)
that's my code :
import 'package:flutter/material.dart';
void main
{
runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey[700],
appBar: AppBar(
title: Text('I am Rich'),
backgroundColor: Colors.blueGrey[900],
),
body: Center(
child: Image(
image: NetworkImage('https://picsum.photos/250?image=9'),
),
),
),
),
);
}

The method 'get' was called on null when using stream builder. What is wrong in the code?

I am building a mobile application using mvp in flutter. The problem i am facing is that the code i used to make the product screen and order screen is same, using stream builder. But in case of Product screen the code is working perfectly fine showing circular Progress indicator but when i navigate to order screen it first show me an error on order screen instead of circular progress indicator and after some milli seconds when it get the response it shows the result. I also attached the video here for better understanding. https://www.youtube.com/watch?v=X4QX1-sKS9k
The init state method and build method of stateful widget are also same but i am not getting the problem where i am doing wrong in the code. The code of build state of order screen is below which is causing problem.
Build method of order:
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.orange,
automaticallyImplyLeading: false,
title: Text(
'Orders',
style: TextStyle(color: Colors.white),
),
),
body: widget.myList.isNotEmpty //TODO update it remove the parameters
? orderListView(widget.myList)
: StreamBuilder(
stream: orderPresenter.getOrders,
builder: (context, AsyncSnapshot<OrdersList> snapshot) {
if (snapshot.hasData) {
return orderListView(
snapshot.data.orders //TODO update it remove the parameters
);
} else if (snapshot.data.orders.isEmpty) {
return Center(child: Text('Your orders are empty'),);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
return Center(
child: Container(
child: CircularProgressIndicator(),
),
);
},
),
);
}
In Order Presenter we have
final _ordersFetcher = PublishSubject<OrdersList>();
#override
Observable<OrdersList> get getOrders => _ordersFetcher.stream;
Solution that worked for me:
if (!snapshot.hasData) {
return Center(
child: Container(
child: CircularProgressIndicator(),
),
);
}else
if(snapshot.hasData){
orderListView(snapshot.data
.orders);
}
You aren't checking for a case in which snapshot.data.orders could be null. That's what the error in your video is showing. Check for null and show the CircularProgressIndicator() while it is null. You can add it to your first if and it is null it will default to your already present CircularProgressIndicator() at the end of the StreamBilder:
if (snapshot.hasData || snapshot.data.orders != null) {
return orderListView(
snapshot.data.orders //TODO update it remove the parameters
);
}

Reading the contents of a file into a String variable in Flutter

The problem is this: I have a .txt file that contains one line of text and I need to read that line into a String variable.
Most of the methods I found return either a Future or Future and I have no idea how to convert these types into Strings. Also, I'm not sure what I'm doing wrong with readAsStringSync because I get a "FileSystemExeption: Cannot open file (OS Error: No such file or directory)", even though I have referenced it in my pubspec.yaml
class LessonPage extends StatelessWidget { LessonPage({this.title, this.appBarColor, this.barTitleColor, this.fileName});
final String title;
final Color appBarColor;
final Color barTitleColor;
final String fileName;
#override
Widget build(BuildContext context) {
final file = new File(this.fileName);
return new Scaffold(
appBar: new AppBar(
title: new Text(
this.title,
style: new TextStyle(color: this.barTitleColor)
),
backgroundColor: this.appBarColor,
),
body: new Center(
child: new Text(
file.readAsStringSync(),
softWrap: true,
)
),
);
Embrace Futures! This use case is exactly what FutureBuilder was made for.
To read assets as strings, you don't need to construct a File. Instead, use DefaultAssetBundle to access asset files. Make sure that the asset file you want to read is declared in pubspec.yaml.
return new Scaffold(
appBar: new AppBar(
title: new Text(
this.title,
style: new TextStyle(color: this.barTitleColor)
),
backgroundColor: this.appBarColor,
),
body: new Center(
child: new FutureBuilder(
future: DefaultAssetBundle.of(context).loadString(fileName),
builder: (context, snapshot) {
return new Text(snapshot.data ?? '', softWrap: true);
}
),
),
);
If you're reading a file that isn't an asset (for example, a file you downloaded to a temporary folder) then it's appropriate to use a File. In that case, make sure that the path is correct. Consider using FutureBuilder instead of the synchronous File APIs, for better performance.