Flutter:Dart Cast Error空值检查运算符用于空值

问题描述

我是 Flutter 的新手,我正在尝试打开相机,为此我遵循了示例代码,但是每当我调试我的应用程序时,它都会给我异常,如图

Error Image

这是我的代码

import 'dart:async';
import 'package:Flutter/material.dart';
import 'package:camera/camera.dart';
import 'home.dart';

List<CameraDescription> cameras;

Future<Null> main() async {
  try {
    cameras = await availableCameras();
  } on CameraException catch (e) {
    print('Error: $e.code\nError Message: $e.message');
  }
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,title: 'RealTime Detection',home: HomePage(cameras),);
  }
}

解决方法

你可以这样做:

let words = "is2 Thi1s T4est 3a";
let wordsArr = words.split(" ");
let result = [];
let r = /\d+/g;

outerLoop: // this is a "label" which we can use to reference this for loop
for (let i = 1; i <= wordsArr.length; i++) { // loop 1 to 4,same as the numbers in text
  innerLoop:
  for (let word of wordsArr) { // for each word
    let match = word.match(r); // get the match result
    if (match && match[0] == i) { // if match found,and the part that matches is our next i
      result.push(word); // add word to array
      continue outerLoop; // go to next i
    }
  }
}

console.log(result.join(" "));