Visual Studio Code randomly gives an error that a class is missing in my code. The class is first imported successfully from the class file into the code. But when I close the file importing it and reopen it, it loses the reference of that imported file and says that the referenced class file could not be found, although it found it before.
I'm creating a widget PersonalActionPlanBox that needs to import the widget PersonalActionPlanRowPreviewBox from file PersonalActionPlanRowPreviewBox.dart. I have already tried to find an answer by Googling and here on Stackoverflow but did not find a clear solution. I have tried importing the widget with both formats like package:... and the relative ../…. But none work.
The widget importing the PersonalActionPlanRowPreviewBox giving the reference error:
import '../PersonalActionPlanRowPreviewBox.dart';
class PersonalActionPlanBox extends StatefulWidget {
final PersonalActionPlan actionPlan;
final UserModel user;
PersonalActionPlanBox({this.actionPlan, this.user});
#override
PersonalActionPlanBoxState createState() => PersonalActionPlanBoxState();
}
class PersonalActionPlanBoxState extends State<PersonalActionPlanBox> {
...
List<Widget> buildRowPreviewBoxes() {
return widget.actionPlan.rows.map((row) {
return new PersonalActionPlanRowPreviewBox (
actionPlanRow: row,
deleteRowCallback: deleteRowEvent,
refreshFollowUpDateCallback: refreshFollowUpDate,
saveActionPlanCallback: saveActionPlan
);
}).toList();
}
...
}
The imported widget PersonalActionPlanRowPreviewBox:
class PersonalActionPlanRowPreviewBox extends StatefulWidget {
final PersonalActionPlanRow actionPlanRow;
final DeleteRowEvent deleteRowCallback;
final RefreshFollowUpDateCallback refreshFollowUpDateCallback;
final SaveActionPlanCallback saveActionPlanCallback;
PersonalActionPlanRowPreviewBox({this.actionPlanRow, this.deleteRowCallback, this.refreshFollowUpDateCallback, this.saveActionPlanCallback});
#override
PersonalActionPlanRowPreviewBoxState createState() => PersonalActionPlanRowPreviewBoxState();
}
class PersonalActionPlanRowPreviewBoxState extends
State<PersonalActionPlanRowPreviewBox> {
...
}
I expect Visual Studio Code to not randomly lose the import/reference to PersonalActionPlanPreviewBox.dart.
Related
I chose riverpod as my state management library. I am reading the official documentation and writing the code. However, the code can't find ConsumerStatefulWidget and ConsumerState, so it keeps showing a red line. When I hover my mouse over it, a suggestion pops up: "Would you like to create a class named 'ConsumerStatefulWidget?'"
import 'package:riverpod/riverpod.dart';
import 'package:flutter/material.dart';
class MyHomePage extends ConsumerStatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends ConsumerState<MyHomePage> {
#override
Widget build(BuildContext context) {
return Container();
}
}
I tried pub get several times after flutter clean, but nothing changed.
It's a simple code that shouldn't be complicated, but I'm not sure why this problem occurs.
I just found out That I should use the flutter_riverpod package, not the riverpod package.
I have created a widget in Flutter as follows:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class WidgetA<T> extends StatefulWidget {
const WidgetA({
super.key,
required this.errorSelector,
...
});
final Function errorSelector;
...
#override
State<WidgetA> createState() =>
_WidgetAState<T>();
}
class _WidgetAState<T> extends State<WidgetA> {
...
#override
Widget build(BuildContext context) {
...
return Builder(
builder: (ctx) {
final String foo =
ctx.select(widget.errorSelector as String Function(T));
return Text(foo);
}
);
}
}
Is this practice okay?
Are there better ways to accomplish this?
Can this cause any issues?
FYI - T is being used to pass a Class that extends a Bloc.
it's not bad and it's not good unless you have a good reason to do it (for example if you want to customize a data type based on that generic ).
so before doing it, ask yourself why so I need to make it generic, all the good patterns in the code are there to add some value to the code.
as you can see the only place where the generic is important is to set it in the State object, this prevents conflicting your StatefulWidget in your app with others and specify it to one StatefulWidget
right now I am giving myself a first dive into Flutter animations. What I need is animation of a match getting burnt. What can I use to make it?
You can create such animations in Flutter Flare. Check out https://pub.dev/packages/flare_flutter
Create an animation on https://flare.rive.app/ and export using the export engine, or you can find ready resources on https://flare.rive.app/explore/popular/trending/all
once you get the .flr file from there, your can import it into your flutter app using the flare_flutter plugin.
import 'package:flare_flutter/flare_actor.dart';
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return new FlareActor("assets/Filip.flr", alignment:Alignment.center, fit:BoxFit.contain, animation:"idle");
}
}
I'm working on my first Flutter project having spent many years on Objective-C and Swift so I'm very much learning the quirks of Dart vs the languages I'm familiar with. Currently I'm trying to follow the patterns I'm seeing in Dart and Flutter to add a builder to a class like this:
// Type def of builder closure.
typedef RecordWidgetBuilder<T> = Widget Function(#required T);
// Widget to display a list of records
class GroupedByDateWidget<T> extends StatefulWidget {
final List<T> items;
final RecordWidgetBuilder<T> widgetBuilder;
GroupedByDateWidget({#required this.items, #required this.widgetBuilder});
#override
_GroupedByDateWidgetState createState() => _GroupedByDateWidgetState(items: items, widgetBuilder: widgetBuilder);
}
class _GroupedByDateWidgetState<Item> extends State<GroupedByDateWidget> {
final List<Item> items;
final RecordWidgetBuilder<Item> widgetBuilder;
_GroupedByDateWidgetState({#required this.items, #required this.widgetBuilder});
// ... Rest of class
}
I then have this widget class that I want the closure to use:
// Widget that displays a record.
class SummarisedWorkRecordWidget extends StatelessWidget {
final WorkRecord record;
SummarisedWorkRecordWidget(this.record);
// ... rest of class
Then I'm trying to use these classes like this:
class _RecordViewState extends State<RecordView> {
#override
Widget build(BuildContext context) {
return GroupedByDateWidget<WorkRecord>(
items: [WorkRecord(), WorkRecord()],
widgetBuilder: (record) => SummarisedWorkRecordWidget(record),
);
}
}
It compiles, but when I run this I get this error:
The following _TypeError was thrown building RecordView(state: _RecordViewState#ade05):
type '(WorkRecord) => SummarisedWorkRecordWidget' is not a subtype of type '(dynamic) => Widget'
The relevant error-causing widget was:
RecordView file:///Users/derekclarkson/Work/projects/FWO/record_my_hours/lib/HomePage.dart:37:13
When the exception was thrown, this was the stack:
#0 GroupedByDateWidget.createState (package:record_my_hours/widgets/GroupedByDateWidget.dart:17:24)
#1 new StatefulElement (package:flutter/src/widgets/framework.dart:4764:24)
...
At this point I'm assuming that closures cannot match on inherited types, but that seem to clash with what I'm seeing in the APIs. Can anyone shed some light on why this isn't working an how to address it?
Solved it. The problem turned out to be that when I created the _GroupedByDateWidget State instance I didn't also specify the generic qualification to it that I'd added. So it instantiated a state with an dynamic generic and failed.
So:
#override
_GroupedByDateWidgetState createState() => _GroupedByDateWidgetState(items: items, widgetBuilder: widgetBuilder);
Should have been:
#override
_GroupedByDateWidgetState createState() => _GroupedByDateWidgetState<Item>(items: items, widgetBuilder: widgetBuilder);
Small thing.
am looking to clean my code
such : menu items it's at every page how to set it
into one dart file and include it later !!
like PHP
<?php include('1.dart'); ?>
am looking to do it with duplicate Containers at pages
that's possible ?
Not exactly like that,but you can make a Widget that suits your needs,then create an instance of it in other widgets by importing the .dart file and using it without writing all its code.
I can give you an example:
I need a specific Container or Card of any other widget that will need to be replicated.What I would do in this scenerio is create a new .dart file and name it after what I need-for example a Social Network post:
class Post extends StatefulWidget {
Post();
#override
_PostState createState() => _PostState();
}
class _PostState extends State<Post> {
_PostState(){
}
#override
Widget build(BuildContext context) {
}
I would write the code that I need to replicate in here then import it inside my home.dart like this:
import 'package:app/widgets/post.dart';
class Home extends StatefulWidget {
Home();
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
_HomeState(){
}
#override
Widget build(BuildContext context) {
Post post = new Post(//here you can construct the post with your data);
}