0

I am coming from React and trying to understand Flutter. There are some differences between dart vs JS and React vs Flutter. However, it seems a bit similar to each other. However, there is some confusion toward Flutter.

  1. void

It does not return the value. Therefore, it is used to void main(). Does it mean like the function 'main()' will be executed and I will never be able to reuse void main()?

  1. <>

In the code, I have here

class LoginScreen extends StatefulWidget {
  createState() {
    return new LoginScreenState();
  }
}

class LoginScreenState extends State<LoginScreen> {
  Widget build(context) {
    return Container(
        margin: EdgeInsets.all(20.0),
        child: Column(
          children: [
            emailField(),
            // passwordField(),
            // submitButton(),
          ],
        ));
  }
}

I can see that State<LoginScreen> is related to class LoginScreen. It is an extension of the LoginScreen State?

Future<int> successor = future.then((int value) {
  
  },
 

However, in here Future <----- I do not understand why it has to state 'int' in this code. What is the proper use of <>?

  1. Stateful vs Stateless

Can I just use the Stateful all the time even if I do not have any state like React? Where there is no difference?

1
  • That main() returns void has nothing to do with it being reusable or not. It's called for its side-effect, just like other functions that returnvoid (e.g. print). Normally you wouldn't want or need to call main() again, though. Commented Oct 28, 2020 at 18:47

2 Answers 2

2

You need to learn basic dart.

  1. void main() is same as main(). The only difference is declaring the return type. main() is called only once by the flutter engine.
  2. Read about generics. <> is the called the diamond operator and is used to specify the type of something. Future<int> means the function will return something in future and it will strictly be of int type. If you don't specify the type flutter will treat it as dynamic.
  3. You can use stateful classes if you are using state management packages like provider or rxdart to maintain state.
Sign up to request clarification or add additional context in comments.

2 Comments

It is more close to TypeScript, as I see. I am also studying Dart as well. Thank you for the answers!
main() is called automatically once, but it's still a function; it can be called multiple times like any other function. For example, main() could be recursive.
1
class LoginScreen extends StatefulWidget {
  createState() {
    return new LoginScreenState();
  }
}

    enter code here

changed to 
class LoginScreen extends StatefulWidget {
  LoginScreenState createState()=>LoginScreenState();
  }
}

    enter code here

Stateful vs Stateless`enter code here`
stateful has state object and stateless has no state object.using stateful you can changes widget data .on other hand in stateful you cant

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.