Flutter Web中的空指针错误:MaterialApp在启动时导致NullPointerException

huangapple go评论78阅读模式
英文:

Null Error in Flutter Web: MaterialApp causing NullPointerException on start

问题

我在我的Flutter Web应用程序中遇到了一个与MaterialApp小部件相关的空值错误。当我运行应用程序时,立即收到一个指向第30行的NullPointerException,该行是MaterialApp小部件开始的地方。自从我添加了路由部分以来,我一直遇到这个错误。

import 'package:flutter/material.dart';
// 其他导入

void main() {
  runApp(RateMyPicApp());
}

class RateMyPicApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'RateMe',
      theme: ThemeData(
        // 主题配置
      ),
      initialRoute: '/upload',
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case '/rate':
            final Map<String, dynamic> args = settings.arguments as Map<String, dynamic>;
            if (args != null && args.containsKey('id')) {
              final String id = args['id'];
              return MaterialPageRoute(builder: (context) => RatingPage(imageId: id));
            }
            break;
          case '/upload':
          default:
            return MaterialPageRoute(builder: (context) => UploadScreen());
        }
      },
      // 其他属性和配置
    );
  }
}

// 其他类和代码
英文:

I'm encountering a null error in my Flutter web app, specifically related to the MaterialApp widget. When I run the app, I immediately receive a NullPointerException pointing to line 30 where the MaterialApp widget starts. I am experiencing the error since i have added the routing part.

 import &#39;package:flutter/material.dart&#39;;
// Other imports

void main() {
  runApp(RateMyPicApp());
}

class RateMyPicApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: &#39;RateMe&#39;,
      theme: ThemeData(
        // Theme configuration
      ),
      initialRoute: &#39;/upload&#39;,
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case &#39;/rate&#39;:
            final Map&lt;String, dynamic&gt; args = settings.arguments as Map&lt;String, dynamic&gt;;
            if (args != null &amp;&amp; args.containsKey(&#39;id&#39;)) {
              final String id = args[&#39;id&#39;];
              return MaterialPageRoute(builder: (context) =&gt; RatingPage(imageId: id));
            }
            break;
          case &#39;/upload&#39;:
          default:
            return MaterialPageRoute(builder: (context) =&gt; UploadScreen());
        }
      },
      // Other properties and configuration
    );
  }
}

// Other classes and code

答案1

得分: 1

尝试这个解决方案:只需在末尾添加null

import 'package:flutter/material.dart';
import 'package:user_interface_1/temp1.dart';
import 'package:user_interface_1/temp2.dart';

void main() {
  runApp(RateMyPicApp());
}

class RateMyPicApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'RateMe',
      theme: ThemeData(
          // 主题配置
      ),
      initialRoute: '/upload',
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case '/rate':
            final Map<String, dynamic> args =
                settings.arguments as Map<String, dynamic>;
            if (args != null && args.containsKey('id')) {
              final String id = args['id'];
              return MaterialPageRoute(builder: (context) => RatingPage());
            }
            break;
          case '/upload':
          default:
            return MaterialPageRoute(builder: (context) => UploadScreen());
        }
        return null;
      },
    );
  }
}

这是你提供的代码的中文翻译部分。

英文:

Try this Solution: Just add null at the end:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:user_interface_1/temp1.dart&#39;;
import &#39;package:user_interface_1/temp2.dart&#39;;

void main() {
  runApp(RateMyPicApp());
}


class RateMyPicApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: &#39;RateMe&#39;,
  theme: ThemeData(
      // Theme configuration
      ),
  initialRoute: &#39;/upload&#39;,
  onGenerateRoute: (settings) {
    switch (settings.name) {
      case &#39;/rate&#39;:
        final Map&lt;String, dynamic&gt; args =
            settings.arguments as Map&lt;String, dynamic&gt;;
        if (args != null &amp;&amp; args.containsKey(&#39;id&#39;)) {
          final String id = args[&#39;id&#39;];
          return MaterialPageRoute(builder: (context) =&gt; RatingPage());
        }
        break;
      case &#39;/upload&#39;:
      default:
        return MaterialPageRoute(builder: (context) =&gt; UploadScreen());
    }
    return null;
  },
);
 }
}

huangapple
  • 本文由 发表于 2023年5月18日 02:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定