I'm stuck on Flutter/Dart Null Safety Issue

Viewed 33

I can't solve this null safety issue. I've tried a million options, and I'm still getting an error. Any help would be appreciated.

Here's the error. It keeps saying I'm I'm passing a String? to a String, but I don't see how?

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
                      data: _pageManager.returnMarkdown(widget.documentName!)),

Here's the code that produces the error:

class MarkdownWidget extends StatefulWidget {
  const MarkdownWidget({
    Key? key,
    this.width,
    this.height,
    this.documentName,
  }) : super(key: key);

  final double? width;
  final double? height;
  final String? documentName;

  @override
  _MarkdownWidgetState createState() => _MarkdownWidgetState();
}

class _MarkdownWidgetState extends State<MarkdownWidget> {
  final PageManager _pageManager = PageManager();
  final scaffoldKey = GlobalKey<ScaffoldState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
      body: SafeArea(
        child: GestureDetector(
          onTap: () => FocusScope.of(context).unfocus(),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              Container(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height * 1,
                decoration: BoxDecoration(),
                child: Padding(
                  padding: EdgeInsetsDirectional.fromSTEB(5, 80, 5, 0),
                  child: Markdown(
                      data: _pageManager.returnMarkdown(widget.documentName!)),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class PageManager {
  String? privacy = """

# Privacy Policy

""";

  String? terms = """
    
# Agreement To Terms of Use
                                                                                                       
""";

  String? returnMarkdown(String? pageName) {
    if (pageName! == null) {
      
      return 'Nothing to see here.';
      
    }
    switch (pageName) {
      case 'Privacy':
        {
          return privacy;
        }

      case 'Terms':
        {
          return terms;
        }
    }
  }
}

Thank you for any help on this issue.

1 Answers

I finally found a solution. I'll post it here in case anyone has a similar issue. I had to change the code in the PageManager.

In short, I removed all of the "null safety" syntax in PageManager. I'm no expert, but I think this works because the argument I'm passing to PageManager is already using the null safety syntax.

Additionally, I added a default statement to the switch statement.

class PageManager {
  String privacy = """

# Privacy Policy


""";

  String terms = """
    
# Agreement To Terms of Use

                                                                                                       
""";

  String returnMarkdown(String pageName) {
   
    switch (pageName) {
      case 'Privacy':
        {
          return privacy;
        }

      case 'Terms':
        {
          return terms;
        }
        
      default:
        {
          return "Nothing to see here";
        }
    }
  }
}
Related