This my code for a app i am trying to make but whenever i am running the code using flutter run command in vs code terminal, Error: Method not found comes for web and mobile in the main code The main code is
import 'package:flutter/material.dart';
import 'utility/colors.dart';
import 'responsive/responsive_layout_screen.dart';
import 'responsive/mobile_screen_layout.dart';
import 'responsive/webscreen_layout.dart';
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(
debugShowCheckedModeBanner: false,
title: 'E-Shopping',
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: mobileBackgroundColor,
),
home: const Layout(WebLayout: web(), MobileLayout: mobile()),
);
}
}
Here is my layout program
import 'package:flutter/material.dart';
import '../utility/dimension.dart';
class Layout extends StatelessWidget {
final Widget WebLayout;
final Widget MobileLayout;
const Layout({
Key? key,
required this.WebLayout,
required this.MobileLayout,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
if (constraints.maxWidth \> 600) {
return WebLayout;
} else {
return MobileLayout;
}
});
}
}
This is my web Screen layout code
import 'package:flutter/material.dart';
class web extends StatelessWidget {
const web({ Key? key }) : super(key: key);
@override
Widget build(BuildContext context) {
// ignore: prefer_const_constructors
return Scaffold(
body: const Center(child: Text("web")),
);
}
}
This is my mobile screen layout Code
import 'package:flutter/material.dart';
class mobile extends StatelessWidget {
const mobile({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// ignore: prefer_const_constructors
return Scaffold(
body: const Center(child: Text("mobile")),
);
}
}
Whenever I am running the code the following error is coming
lib/main.dart:23:37: Error: Method not found: 'web'.
home: const Layout(WebLayout: web(), MobileLayout: mobile()),
^^^
lib/main.dart:23:58: Error: Method not found: 'mobile'.
home: const Layout(WebLayout: web(), MobileLayout: mobile()),
^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script 'C:\\flutter\\packages\\flutter_tools\\gradle\\flutter.gradle' line: 1102
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\\flutter\\bin\\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 27s
I have tried restarting vs code and the simulator but the error is still coming.
CodePudding user response:
- Make your classes start with a capital
- Import the files for each class into the file you need to use it
- Properties are camelCase starting with lowercase
This will help improve the readability of the app and make it easier for us to see if you're trying to call a function (which starts with lower case) or a Class constructor which starts with upper case.
Once you update that in your code and it still doesn't work leave a comment and I'll come edit my answer with your updated answer in mind.