I’m cleaning up my Flutter code and I’m not sure if I’m writing my Flutter code in the correct style.
First, here’s my code to import the file:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:project/src/common_widgets/custom_text_form_field.dart';
import 'package:project/src/common_widgets/generic_button.dart';
Here is the code for my StatelessWidget and StatefulWidget:
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) => Container();
}
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
void name(params) {}
return Container();
}
}
class MyWidget extends StatefulWidget {
const MyWidget({super.key});
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) => Container();
}
class MyWidget extends StatefulWidget {
const MyWidget({super.key});
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
void name(params) {}
return Container();
}
}
Feel free to comment if you need more code to check it out!
Am I writing Flutter code in the correct style? I would appreciate any help. Thank you in advance!
>Solution :
Note, I’m unsure if these types of questions are suitable for StackOverflow, they might be considered as "opinion based" or "off-topic". You might want to consider codereview.stackexchange.
You haven’t really shared enough code, but:
Imports
Your import should be relative imports (see Flutter imports : relative path or package?
).
meaning, instead of:
import 'package:project/src/common_widgets/custom_text_form_field.dart';
should be:
import './project/src/common_widgets/custom_text_form_field.dart';
Functions
Your function:
void name(params) {}
is defined within your build method, so every rebuild, the entire function will be recreated, and additionally, any values set in there will be reset every rebuild, i.e. every setState