flutter The expression doesn't evaluate to a function, so it can't be invoked

Issue

am trying to do a login screen to my application and I wanted to make a loading screen using flutter spinkit but there s always an error in my loading function

the error is at the return loading ? loading() The expression doesn’t evaluate to a function, so it can’t be invoked

if anyone can help me It would be great and thank you in advance

// ignore_for_file: use_key_in_widget_constructors, prefer_const_literals_to_create_immutables, prefer_const_constructors, unused_import, non_constant_identifier_names
import 'dart:ffi';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:myapp2/loading.dart';
import 'package:myapp2/second.dart';
import 'dart:convert';

void main() {
 runApp(const MyApp());
}

class MyApp extends StatelessWidget {
 const MyApp({Key? key}) : super(key: key);

 // This widget is the root of your application.
 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     title: 'Flutter Demo',
     theme: ThemeData(
       primarySwatch: Colors.blue,
     ),
     home: Login(),
   );
 }
}

class Login extends StatefulWidget {
 @override
 LoginPage createState() => LoginPage();
}

class LoginPage extends State<Login> {
 var emailControler = TextEditingController();
 var passControler = TextEditingController();
 bool Loading = false;

 @override
 Widget build(BuildContext context) {
   return Loading
       ? Loading()
       : Scaffold(
           body: Padding(
             padding: const EdgeInsets.all(10.0),
             child: SafeArea(
               child: Center(
                   child: Column(
                 mainAxisAlignment: MainAxisAlignment.center,
                 children: [
                   TextFormField(
                     controller: emailControler,
                     obscureText: false,
                     decoration: InputDecoration(
                         labelText: "Email",
                         border: OutlineInputBorder(),
                         suffixIcon: Icon(Icons.email)),
                   ),
                   SizedBox(
                     height: 15,
                   ),
                   TextFormField(
                     controller: passControler,
                     obscureText: true,
                     decoration: InputDecoration(
                         labelText: "Password",
                         border: OutlineInputBorder(),
                         suffixIcon: Icon(Icons.lock)),
                   ),
                   SizedBox(
                     height: 45,
                   ),
                   OutlinedButton.icon(
                       onPressed: () {
                         Login();
                       },
                       icon: Icon(
                         Icons.login,
                         size: 18,
                       ),
                       label: Text("login")),
                 ],
               )),
             ),
           ),
         );
 }

 // base64 encoding a string

 encode() {
   String credentials = emailControler.text + ":" + passControler.text;
   Codec<String, String> stringToBase64 = utf8.fuse(base64);
   String encoded = stringToBase64.encode(credentials);
   return (encoded);
 }

 //Login POST API CALL
 Future<void> Login() async {
   if (passControler.text.isNotEmpty && emailControler.text.isNotEmpty) {
     setState(() => Loading = true);
     var response = await http.post(
         Uri.parse(""),
         headers: ({
           'MAXAUTH': encode(),
         }));
     if (response.statusCode == 200) {
       setState(() => Loading = true);
       Navigator.push(
           context, MaterialPageRoute(builder: (context) => Second()));
     } else {
       setState(() => Loading = false);
       ScaffoldMessenger.of(context)
           .showSnackBar(SnackBar(content: Text("Invalid cords!")));
     }
   } else {
     setState(() => Loading = false);
     ScaffoldMessenger.of(context).showSnackBar(
         SnackBar(content: Text("Empty fields are not allowed !")));
   }
 }
}

Solution

loading is defined as a boolean in your code. It can’t be used as a widget! The naming convention is to use camelCase/lowercase when defining variables to avoid confusion.

Change

 bool Loading = false;

To

bool loading = false;

Then update your code:

return loading
   ? Center(
        child: SpinKitRotatingCircle(
           color: Colors.blue,
           size: 50.0,
        ),
     );
   : Scaffold(
       body: Padding(

Answered By – Marcos Maliki

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *