Home > Net >  What does " int? get priority => 1;" do?
What does " int? get priority => 1;" do?

Time:04-23

I was reading a flutter code as below:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:test_get_app/user_controller.dart';

class AuthMiddleware extends GetMiddleware {
  final authService = UserController.findOrInitialize; // Here is error, this line can't find UserController
  @override
  int? get priority => 1;
  bool isAuthenticated = false;

  @override
  RouteSettings? redirect(String? route) {
    isAuthenticated = true;
    if (isAuthenticated == false) {
      return const RouteSettings(name: '/login');
    }
    return null;
  }
}

When I reached to the following line, I couldn't understand it's syntax and how does it work?

  int? get priority => 1;

CodePudding user response:

  • int? Means it is an int but the int can be null

  • => 1 Means () {return 1;}

CodePudding user response:

This is a so-called getter. Getters can be used to provide read access to class properties.

They can also return values directly, like in your case.

They are accessed like properties of the class they are declared in:

final middleWare = AuthMiddleware();
final priority = middleWare.priority;

In your case the getter probably must or can be implemented (see the @override annotation), since all implementations of a middleware must declare their priority, I guess. Since the declared type is int? it may also return null instead of an integer.

Getters can be declared using an expression. Like in your case. Using a block body does also work:

int? get priority {
  return 1;
}
  • Related