1
\$\begingroup\$

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?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What does your code do? \$\endgroup\$
    – BCdotWEB
    Jan 2 at 22:01
  • \$\begingroup\$ @BCdotWEB sorry, I don't understand what you're talking about \$\endgroup\$
    – My Car
    Jan 2 at 23:33
  • 2
    \$\begingroup\$ What does your code do? Does it summon spirits, is it a recipe for a banana milkshake? The current title of your question is too generic to be helpful. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    Jan 3 at 8:04
  • 1
    \$\begingroup\$ Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Generic best practices are outside the scope of this site. Please follow the tour, and read "What topics can I ask about here?", "How do I ask a good question?" and "What types of questions should I avoid asking?". \$\endgroup\$
    – BCdotWEB
    Jan 3 at 8:04

1 Answer 1

3
\$\begingroup\$

Note, the reason I was able to answer so quickly is because this was originally asked on StackOverflow (now deleted) - to which I redirected the OP to here.

You haven't really shared enough code, but:

Imports

Your imports 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 for every rebuild, the entire function will be recreated. And additionally, any values set in there will be reset every rebuild, i.e. every setState.

Now, your example was with a function, but I commonly see this mistake of holding variables within the build method. Take for example:

Widget build(BuildContext context) {
    var _value = false;
    return Switch(
        value: _value,
        onChanged: (value) {
          setState(() {
            _value = value;
          });
        });
  }

The value of _value will never be changed from false to true since every update of the UI - (calling setState) would just reset the value to false.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.