编译空值时崩溃,字符偏移为空- Flutter和Dart

8hhllhi2  于 2022-11-25  发布在  Flutter
关注(0)|答案(4)|浏览(125)

我是新的Flutter和飞镖,我试图使一个克隆的Instagram的学习目的,但我得到下面的错误。
你能帮我找出密码不对的地方吗?
输出消息错误位于末尾。
我试过谷歌,并重构代码ir以应用空安全,但在现实中,我不知道确切的地方是错误的代码。

主省道

import 'package:flutter/material.dart';
import 'package:instagram_flutter/responsive/mobile_screen_layout.dart';
import 'package:instagram_flutter/responsive/responsive_layout_screen.dart';
import 'package:instagram_flutter/responsive/web_screen_layout.dart';
import 'package:instagram_flutter/utils/colors.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Instagram Clone',
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: mobileBackgroundColor,
      ),
      home: ResponsiveLayout(
        webScreenLayout: WebScreenLayout(),
        mobileScreenLayout: MobileScreenLayout(),
      ),
    );
  }
}

移动屏幕布局.dart

import 'package:flutter/material.dart';

class MobileScreenLayout extends StatelessWidget {
  const MobileScreenLayout({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('This is mobile'),
      ),
    );
  }
}

响应布局屏幕.dart

在下面的IF ELSE中,我试着用一些方法来验证值是零还是空,但没有成功。我还试着在constrainsts.maxWidth的末尾加上?,因为我在我的研究中看到这可以避免空值,但也没有成功。

import 'package:flutter/material.dart';
import 'package:instagram_flutter/responsive/web_screen_layout.dart';
import 'package:instagram_flutter/utils/dimensions.dart';

class ResponsiveLayout extends StatelessWidget {
  final Widget webScreenLayout;
  final Widget mobileScreenLayout;

  const ResponsiveLayout({
    super.key,
    required this.webScreenLayout,
    required this.mobileScreenLayout,
  });

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        if (constraints.maxWidth > webScreenSize) {
          return webScreenLayout;
        }
        return mobileScreenLayout;
      },
    );
  }
}

网页屏幕布局.dart

import 'package:flutter/material.dart';

class WebScreenLayout extends StatelessWidget {
  const WebScreenLayout({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('This is web'),
      ),
    );
  }
}

颜色.省道

import 'package:flutter/material.dart';

const mobileBackgroundColor = Color.fromRGBO(0, 0, 0, 1);
const webBackgroundColor = Color.fromRGBO(18, 18, 18, 1);
const mobileSearchColor = Color.fromRGBO(38, 38, 38, 1);
const blueColor = Color.fromRGBO(0, 149, 246, 1);
const primaryColor = Colors.white;
const secondaryColor = Colors.grey;

尺寸.省道

import 'package:flutter/material.dart';
import 'package:instagram_flutter/responsive/web_screen_layout.dart';

const webScreenSize = 600;

输出

[Running] dart "c:\Development\instagram_flutter\lib\main.dart"
Crash when compiling null,
at character offset null:
Null check operator used on a null value
#0      InferableTypeBuilderMixin.type (package:front_end/src/fasta/builder/type_builder.dart:392:29)
#1      InferableTypeBuilder.inferType (package:front_end/src/fasta/builder/omitted_type_builder.dart:155:12)
#2      SourceLoader.performTopLevelInference (package:front_end/src/fasta/source/source_loader.dart:2358:19)
#3      KernelTarget.buildOutlines.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:532:14)
<asynchronous suspension>
#4      withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
#5      _buildInternal (package:front_end/src/kernel_generator_impl.dart:139:7)
<asynchronous suspension>
#6      withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
#7      generateKernel.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:47:12)
<asynchronous suspension>
#8      generateKernel (package:front_end/src/kernel_generator_impl.dart:46:10)
<asynchronous suspension>
#9      kernelForModule (package:front_end/src/api_prototype/kernel_generator.dart:100:11)
<asynchronous suspension>
#10     SingleShotCompilerWrapper.compileInternal (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:412:11)
<asynchronous suspension>
#11     Compiler.compile.<anonymous closure> (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:221:45)
<asynchronous suspension>
#12     _processLoadRequest (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:914:37)
<asynchronous suspension>

#0      InferableTypeBuilderMixin.type (package:front_end/src/fasta/builder/type_builder.dart:392:29)
#1      InferableTypeBuilder.inferType (package:front_end/src/fasta/builder/omitted_type_builder.dart:155:12)
#2      SourceLoader.performTopLevelInference (package:front_end/src/fasta/source/source_loader.dart:2358:19)
#3      KernelTarget.buildOutlines.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:532:14)
<asynchronous suspension>
#4      withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
#5      _buildInternal (package:front_end/src/kernel_generator_impl.dart:139:7)
<asynchronous suspension>
#6      withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
#7      generateKernel.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:47:12)
<asynchronous suspension>
#8      generateKernel (package:front_end/src/kernel_generator_impl.dart:46:10)
<asynchronous suspension>
#9      kernelForModule (package:front_end/src/api_prototype/kernel_generator.dart:100:11)
<asynchronous suspension>
#10     SingleShotCompilerWrapper.compileInternal (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:412:11)
<asynchronous suspension>
#11     Compiler.compile.<anonymous closure> (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:221:45)
<asynchronous suspension>
#12     _processLoadRequest (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:914:37)
<asynchronous suspension>

[Done] exited with code=252 in 5.039 seconds
aurhwmvo

aurhwmvo1#

经过3天的搜索,我能够解决这个问题,只是通过删除这个Extension,如果你没有这个扩展,但问题仍然存在,那么问题肯定是一个扩展,尝试禁用它一个接一个,并重新加载vscode,直到你找到它

htrmnn0y

htrmnn0y2#

问题来自ResponsiveLayout

if (constraints.maxWidth > webScreenSize?) { //here
  return webScreenLayout;
}

删除?并执行

if (constraints.maxWidth > webScreenSize) {  
  return webScreenLayout;
}
sshcrbum

sshcrbum3#

禁用coderunner扩展并运行flutter upgrade,如果它不起作用,运行flutter clean,然后在终端中运行flutter pub get,它对我有效,希望对你也有效

euoag5mw

euoag5mw4#

禁用coderunner扩展并重新启动您的工作区/vs代码。它对我有效,希望对您也有效。

相关问题