frzi / swiftui-router

Path-based routing in SwiftUI
MIT License
900 stars 43 forks source link

"No ObservableObject of type Navigator found." caused by xcode preview #29

Closed qtisan closed 2 years ago

qtisan commented 2 years ago

image

As seen, I successfully run with it on my actual device, but failed in preview. Am I made something wrong?

MacOS BigSur 11.6 (20G165) XCode Version 13.0 (13A233) Swift 5.5

Just simple code


import SwiftUI
import SwiftUIRouter

struct Simple: View {

    var body: some View {

      NavLink(to: "/welcome") {
        Text("Enter")
      }

    }
}

struct Simple_Previews: PreviewProvider {
    static var previews: some View {
        Simple()
    }
}

Diagnostics

No ObservableObject of type Navigator found. A View.environmentObject(_:) for Navigator may be missing as an ancestor of this view.

----------------------------------------

MissingEnvironmentObjectError: Preview is missing environment object "Navigator"

[Sample] crashed due to missing environment of type: Navigator. To resolve this add `.environmentObject(Navigator(...))` to the appropriate preview.

Process: [Sample][65512]
Date/Time: 2021-09-29 01:02:26 +0000
Log File: <none>

Application Specific Information:
     [
        dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/[Sample]-dcfjwaohgmozctgpvtbixewazoqo/Build/Intermediates.noindex/Previews/[Sample]/Products/Debug-iphonesimulator DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot//System/Library/PrivateFrameworks/PreviewsInjection.framework/PreviewsInjection DYLD_FRAMEWORK_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/[Sample]-dcfjwaohgmozctgpvtbixewazoqo/Build/Intermediates.noindex/Previews/[Sample]/Products/Debug-iphonesimulator 
        SwiftUI/EnvironmentObject.swift:70: Fatal error: No ObservableObject of type Navigator found. A View.environmentObject(_:) for Navigator may be missing as an ancestor of this view.

        CoreSimulator 776.3 - Device: iPhone 12 Pro (48F2C8C7-D179-42EE-8709-82EB7E969381) - Runtime: iOS 15.0 (19A339) - DeviceType: iPhone 12 Pro

    ]

==================================

|  MessageSendFailure: Message send failure for send previewInstances message to agent
|  
|  ==================================
|  
|  |  RemoteHumanReadableError: Cannot send message on <UVAgentPreviewServiceServerConnection: 0x600001ee54a0>: connection has invalidated
|  |  
|  |  com.apple.dt.UITestingAgent (-1):
frzi commented 2 years ago

When you use Previews, your View will be rendered in a very isolated environment. All SwiftUIRouter Views have to be used somewhere inside a Router.

Try this instead:

struct Simple_Previews: PreviewProvider {
    static var previews: some View {
        Router {
            Simple()
        }
    }
}

All SwiftUIRouter Views (except for Router) depend on several environment objects and values. The Router View initiates all these objects and values. 😃

qtisan commented 2 years ago

Thank you, it looks like nice!