0

my question is about global variables. I think I have never seen variables declared outside of classes. My question is: why?

For my small projects I often created variables simply like that:

import 'package:flutter/material.dart';

String title = 'this is title';
String userName = '';

StatefulWidget...

It always worked for me to easy use them

helloitsme3
  • 209
  • 1
  • 8
  • It is purely opinion and style. A lot of people who have worked in software for a considerable amount of time have found that this type of declaration can lead to a number of issues, especially on larger projects. Therefore, it is generally discouraged. – eimmer Oct 12 '21 at 18:16

1 Answers1

2

Because normally global parameters are mostly used for constants.

Non-const global variables are evil because their value can be changed by any function. Using global variables reduces the modularity and flexibility of the program

Are global variables bad?

As for where to put constants, there are few sugestions: What's the best practice to keep all the constants in Flutter?

Nuts
  • 8,789
  • 1
  • 31
  • 38